您的位置:首页 > 其它

王爽汇编语言课程设计一

2012-10-05 02:30 471 查看
上个学期把王爽老师的《汇编语言》看了一些,做了第一个课程设计,今天再看这本书,又把这道题目做了一遍

assume cs:code, ds:data
data segment
db '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983'
db '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992'
db '1993', '1994', '1995'

;Above are 21 strings representing numbers

dd 16, 22, 382, 1356, 2390, 8000, 16000, 24486, 50065, 97479, 140417, 197514
dd 345980, 590827, 803530, 1183000, 1843000, 2758000, 3753000, 4649000, 5937000

;Above are 21 dwords representing the total income

dw 3, 7, 9, 13, 28, 130, 220, 476, 778, 1001, 1442, 2258, 2793, 4037, 5635, 8226
dw 11542, 14430, 15257, 17800

;Above are the numbers of employee type word
data ends

table segment
db 21 dup ('year summ ne ?? ')
db 16 dup (0)
table ends

code segment
start:  mov ax, data
mov ds, ax

mov ax, table
mov es, ax

mov bx, 0
mov di, 0
mov si, 0

mov cx, 21

s:  mov ax, [di]            ; Year
mov es:[bx], ax
mov ax, [di].2
mov es:[bx].2, ax

mov ax, 0               ; Zero
mov es:[bx].4, ax

mov ax, [di].84         ; Total Income
mov es:[bx].5, ax
mov ax, [di].86
mov es:[bx].7, ax

mov ax, ' '             ; Space
mov es:[bx].9, ax

mov ax, [si].168        ; Stuff Number
mov es:[bx].0ah, ax

mov ax, ' '             ; Space
mov es:[bx].0ch, ax

; Average Income
mov ax, [di].84
mov dx, [di].86
div word ptr [si].168
mov es:[bx].0dh, ax

mov ax, ' '             ; Space
mov es:[bx].0fh, ax

add di, 4
add si, 2
add bx, 16

loop s

;Now I want to use what I have finished
;----------------------------------------------------------------------------;
;----------------------------------------------------------------------------;

mov ax, table
mov ds, ax

mov bx, 0

mov ax, 0b800h
mov es, ax

mov cx, 21
sss: mov si, 0
call show_year

mov si, 40
mov ax, [bx].5
mov dx, [bx].7
call dtoc
call show_str

mov si, 80
mov ax, [bx].0ah
mov dx, 0
call dtoc
call show_str

mov si, 120
mov ax, [bx].0dh
mov dx, 0
call dtoc
call show_str

mov ax, es
add ax, 10
mov es, ax

add bx, 16
loop sss

mov ax, 4c00h
int 21h

;   Function:   show a string end with 0 on the screen

show_str: push cx
push di
push si
push bx

mov di, 0
show:  mov cl, [di].150h
mov ch, 0
jcxz goo
mov es:[si], cl
mov ch, 00000111b
mov es:[si].1, ch
inc di
add si, 2
jmp short show

goo: pop bx
pop si
pop di
pop cx
ret

;   Function:Turn a dword type data to a decimal string end with 0
;   Parameter:  (ax) = Low 16 bits of dword
;               (dx) = High 16 bits of dowrd
;               ds:si point to string address, we directly to the screen
;   Return: None
;   Pay attention the overflow of divide

dtoc:push ax
push bx
push cx
push dx
push si
push ds

mov si, 0

ok: mov cx, 0ah
call divdw

add cx, 30h
push cx
mov cx, ax
jcxz go1

go3:inc si
jmp short ok

go1:mov cx, dx
jcxz go2
jmp go3
go2:inc si
mov byte ptr [si].150h, 0

mov cx, si
mov si, 0

kkk:pop ax
mov [si].150h, al
inc si
loop kkk

pop ds
pop si
pop dx
pop cx
pop bx
pop ax
ret

;   Function:   Show year string on the screen
;   Parameter:  (bx) = year string's index, (es) = Segment Excursion Screen Memory

show_year:  push ax
push cx
push si
push di

mov si, 0
mov di, 0
mov cx, 4
year: mov al, [bx + si]
mov es:[di], al
mov al, 00000111b
mov es:[di].1, al
inc si
add di, 2
loop year

pop di
pop si
pop cx
pop ax
ret

;   Function: divide dword type, dividor is a word type
;   Parameter:(ax) = dword low 16 bits
;             (dx) = doword high 16 bits
;             (cx) = dividor
;   Return:   (dx) = high 16 bits of result, (ax) = low 16 bits of result
;             (cx) = remainder

divdw:  push bx

mov bx, ax   ;We use bx to store the low 16bits of the original
mov ax, dx
mov dx, 0
div cx
push ax
mov ax, bx
div cx
mov cx, dx
pop dx

pop bx
ret

code ends
end start


和上次的方法不一样

上次代码如下

View Code

assume cs:code,ds:data,ss:stack
stack segment

dw 128 dup (0)

stack ends

data segment

db '1975','1976','1977','1978','1979','1980','1981','1982','1983'

db '1984','1985','1986','1987','1988','1989','1990','1991','1992'

db '1993','1994','1995'

dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514

dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000

dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226

dw 11452,14430,15257,17800

db 16 dup (0)

data ends

code segment
start:
mov ax,0600h
mov bh,00
mov cx,0000h
mov dx,184fh
int 10h
mov ah,2
mov bh,0
mov dh,0
mov dl,12
int 10h
以上为调用中断清除屏幕
mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov ah,07h      ; 设定颜色值
mov bx,0
mov si,0
mov cx,21

s:push cx       ;cx入栈保护
mov di,0
mov cx,4

s1:mov al,[si]
mov es:[bx+di],al
mov es:[bx+di].1,ah   ;assigment
inc si
add di,2
loop s1

add bx,160
pop cx
loop s
;-----显示年份----

mov ax,stack
mov ss,ax
mov sp,32
mov ax,data
mov ds,ax

mov bx,0
mov si,0
mov di,0

mov cx,21
s3: mov dx,86[di]
mov ax,84[di]
call dtoc
call show_str
add di,4
add bx,160
loop s3
;-----显示总收入-----

mov bx,20
mov si,0
mov di,0
mov cx,21
s4:mov dx,0
mov ax,168[di]
call dtoc
call show_str
add di,2
add bx,160
loop s4

;-----显示总人数

mov bx,40
mov si,0
mov di,0
mov cx,21
s5:mov dx,86[di]
mov ax,84[di]
div word ptr 168[si] ;计算人均收入,ax保存
mov dx,0                    ;为了调用dtoc子程序,把收入定为32bit
call dtoc
call show_str
add si,2
add di,4
add bx,160
loop s5

;------显示人均收入-----

mov ax,4c00h
int 21h

;;dtcoc功能把数字转化为十进制表示的字符串
;参数:dx =dword高十六位,ax=dword低十六位
dtoc:
push cx
push si         ;turn the dword to decimal system string
mov si,0
ok:call divdw
add cx,30h
push cx
mov cx,ax
jcxz go1

go3:inc si
jmp  short ok

go1:mov cx,dx
jcxz go2                  ; the go1 go2 go3 judge if the quotient is 0
jmp go3                  ; if true,return

go2:inc si
mov byte ptr 210[si],0
mov cx,si
mov si,0
kkk:pop ax
mov 210[si],al
inc si
loop kkk                  ;function :to reverse the number string

pop si
pop cx
ret
;show_str:在当前屏幕上显示字符串
show_str:push cx
push bx
push ax
push si
push di
mov si,0
mov di,0
mov ah,07h
add bx,20
sh:mov cl,210[di]
mov ch,0
jcxz gos
mov al,210[di]
mov es:[bx+si],al
mov es:[bx+si].1,ah
inc di
add si,2
jmp short sh

gos:pop di
pop si
pop ax
pop bx
pop cx
ret

;divdw功能:进行不会溢出的除法运算,被除数为dword型,除数为word型,结果为dword型
;参数:ax=dword型数据的低16位,dx=dword型数据的高16位,cx=除数
;返回:dx=结果的高16位,ax=结果的低16位,cx=余数
divdw:push bx        ;the parament is dx and ax
mov cx,10           ;function: big number divide
mov bx,ax
mov ax,dx
mov dx,0
div cx
push ax
mov ax,bx
div cx              ;  ax store the lower bits of the quotient
mov cx,dx      ;  cx store the remainder
pop dx            ;  dx store the higher bits of quotient
pop bx
ret
code ends
end start


感受最深的一点就是,代码一定要结构化,子程序的设计一定要独立化,不能和主程序本身有变量的交集

期待第二个课程设计,Come on
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: