"Assembly Language" Wang Shuang Experiment 7 (Detailed explanation, friendly to novices!!!)

Topic: Write the data in the data section into the table section, and calculate the income per capita in 21 years (rounded up), and the result is also stored in the table section

assume cs:codesg,ds:data,es:table,ss:stack

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'
; 21 character strings representing 21 years (4 bytes)
dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514
dd 345980,590827,803530,1183000,1843000,2759000,3753000,4649000,5937000
; represents 21 of the company's total income in 21 years dword data (4 bytes)
dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226
dw 11542,11430,15257,17800
; represents the number of employees in the company in 21 years 21 word data (2 bytes)
data ends

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

stack segment
dw 0,0,0,0,0,0,0,0
stack ends

Option 1: Use multiple loops to write data into the table segment

(1). Connect the segment register to each segment

codesg  segment
start:    mov ax,data
          mov ds,ax
          mov ax,table
          mov es,ax
          mov ax,stack
          mov ss,ax//将寄存器cx的值入栈保存,进行下次循环时拿出减一
          mov sp,10H
          //以上为将各个段与相应段寄存器相连

(2). The general steps of the cycle

   //大循环
      mov cx,...
    s:push cx
   //中循环   
      mov cx,...
   s1:push cx
      mov cx,...
   //小循环
   s2:...
      loop s2//最内层循环不用pop cx
  //小循环    
      pop cx
      loop s1
  //中循环          
      pop cx
      loop s
  //大循环    

(3) Pass the year and income into the table respectively. (The number of cycles for the same byte is the same)
where si represents the offset address of the data segment, di represents the offset address of the table segment, and bx represents the starting offset address to be stored in the table segment

      mov bx,0
      mov si,0//用si表示data中偏移地址,di表示table中偏移地址
      mov di,0
      mov cx,2
    s:push cx
     
     //一个大循环(将年份和收入两种数据分别存入table中,对应上面cx=2)
      mov cx,21
   s1:push cx
      mov cx,4
   s2:mov al,ds:[si]//寄存器间接寻址
      mov es:[bx+di],al//寄存器基址变址寻址
      inc si
      inc di
      loop s2//最内层循环
      
      add di,12//stack中数据按行存储(年份和收入为列)且字节为4,所以减去字节数
      pop cx//非最内层循环需要出栈cx
      loop s1
      //一个大循环
      
      mov bx,5         
      pop cx
      loop s

(4) Pass the number of employees into the table. (The byte is 2, the number of cycles is 2, or the word register is used to store it once)
bx represents the starting offset address to be stored in the table segment

       mov bx,0AH
       mov di,0//上段循环si已表示适当data段偏移地址,不需重设si
       mov cx, 21
   s1: mov ax,ds:[si]
       mov es:[bx+di],ax
       add si,2
       add di,10H
       loop s1

(5) Calculate the per capita income (rounded) based on the table segment data and store it. The
number of employees is 2 bytes of data, and the income is 4 bytes of data. Then the commercial ax (word register) obtained by dividing is stored, and the remainder is stored in the high bit of dx

        mov di,5
        mov cx,21
     s: mov dx,es:[di]
        mov ax,es:[di].2//寄存器相对寻址
        div word ptr es:[di].0AH
        mov es:[di].0DH,ax
        add di,10H
        loop s

(6) Tail code

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

Guess you like

Origin blog.csdn.net/Xgggcalled/article/details/113089169