《汇编语言》第三版实验7代码实现

花了大半天的时间,终于把这个作业给做了。

最初的版本大量使用了loop, 可以说是很低效,很小白了。
但一想到这是我目前写到的最复杂的一个汇编程序,还是选择记录下: P。
期待刷完整本后,再来看这段代码, 能被自己蠢哭。

代码实现

这个实验算是前面8章所学的一个小小总结了。

最初的版本;

assume cs:codesg, es:table, ss:stacksg

data segment
db '1975','1976','1977','1978','1979','1980','1981','1982' 
db '1983','1984','1985','1986','1987','1988','1989','1990'
db '1991','1992','1993','1994','1995'
;stands for years from 1975 to 1995

dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514
dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000
;stands for yearly revenue from 1975 to 1995

dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226
dw 11542,14430,15257,17800
;stands for number of employees from 1975 to 1995

data ends

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

stacksg segment
  dw 8 dup (0)
stacksg ends

codesg segment
  start: mov ax, data
         mov ds, ax
     mov bx, 0
     mov bp,0

     mov ax, table
     mov es, ax

     mov ax, stacksg
     mov ss, ax
     mov sp,0
     
     mov cx, 21
      s: push cx 
         mov si, 0
     mov cx, 2
     s0: mov ax, ds:[bp+si] 
         mov es:[bx+si], ax
     add si,2
     loop s0

     pop cx
     add bx, 16
     add bp, 4
     loop s

     mov cx, 21
     mov bx, 0
     s1: push cx
         mov si, 0
     mov cx, 2
     s2: mov ax, ds:[bp+si]
     mov es:[bx+5+si], ax
     mov ax, ds:[bp+si+2]
     mov es:[bx+7+si], ax
     add si, 2
     loop s2
     
     pop cx
     add bx, 16
     add bp, 4
     loop s1

     mov cx, 21
     mov bx, 0
     s3: mov ax, ds:[bp]
     mov es:[bx+10], ax
     add bx, 16
     add bp, 2
     loop s3

     mov cx, 21
     mov bx, 0
     s4: mov ax, es:[bx+5]
         mov dx, es:[bx+7]
     div word ptr es:[bx+10]
     mov es:[bx+13], ax
     add bx, 16
     loop s4

     mov ax, 4c00h
     int 21h
codesg ends
end start

看网上别人的解答,基本用21*4来偏移,两次循环搞定, 我起初也是这么想的,结果做的时候,发现脑子不够用,于是改用了最简单也最低效的方式,惭愧。

重新试着写了一轮,改进后的版本长这样:

assume cs:codesg, es:table, ss:stacksg

data segment
db '1975','1976','1977','1978','1979','1980','1981','1982' 
db '1983','1984','1985','1986','1987','1988','1989','1990'
db '1991','1992','1993','1994','1995'
;stands for years from 1975 to 1995

dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514
dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000
;stands for yearly revenue from 1975 to 1995

dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226
dw 11542,14430,15257,17800
;stands for number of employees from 1975 to 1995

data ends

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

stacksg segment
  dw 8 dup (0)
stacksg ends

codesg segment
  start: mov ax, data
         mov ds, ax
     mov bx, 0
     mov bp, 0
     mov di, 0

     mov ax, table
     mov es, ax

     mov ax, stacksg
     mov ss, ax
     mov sp,0
     
     mov cx, 21
      s: push cx 
         mov si, 0
     mov cx, 2
     s0: mov ax, ds:[bp+si] 
         mov es:[bx+si], ax

     mov ax, ds:[bp+21*4+si]
     mov es:[bx+5+si], ax

     add si,2
     loop s0


         mov ax, ds:[di+21*4+21*4]
     mov es:[bx+10], ax

         mov ax, es:[bx+5]
         mov dx, es:[bx+7]
     div word ptr es:[bx+10]
     mov es:[bx+13], ax

     pop cx
     add bx, 16
     add bp, 4
     add di, 2
     loop s

     mov ax, 4c00h
     int 21h
codesg ends
end start

果然虐过一遍,脑子清醒多了。