Assembly language (Wang Shuang) - Experiment

Posted by verlen on Thu, 24 Feb 2022 15:18:43 +0100

Chapter 7 more flexible method of locating memory address

2. Program and complete the procedure in question 7.9

assume cs:codesg,ss:stacksg,ds:datasg
stacksg segment
  dw 0,0,0,0,0,0,0,0
stacksg ends


datasg segment
  db '1. display      '
  db '2. brows        '
  db '3. replace      '
  db '4. modify       '
datasg ends


codesg segment
 start:mov ax,stacksg
       mov ss,ax
       mov sp,16
       mov ax,datasg
       mov ds,ax
       mov bx,0
       
       mov cx,4
       
   s0:push cx
      mov si,3
      mov cx,4
    s:mov al,[bx+si]
      and al,11011111b
      mov [bx+si],al
      inc si
      loop s
      
      add bx,16
      pop cx
      loop s0
      
      mov ax,4c00h
      int 21h
 codesg ends
 end start
   

Chapter 8 two basic problems of data processing

assume cs:codesg, ss:stack,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'

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 11542, 14430, 15257, 17800
data ends

table segment
db 21 dup ('year summ ne ?? ')
table ends
assume cs:codesg, ss:stack

stack segment
dw 8 dup (0)         ;deposit cx
stack ends

codesg segment
start:mov ax,table
      mov ds,ax
      mov ax,stack
      mov ss,ax
      mov sp,16
      mov ax,data
      mov es,ax
      
      mov cx,21
      mov si,0
      mov bx,0
      
 year:push cx
      mov di,0
      mov cx,4
      
    s:mov al,es:[si]
      mov [bx+di],al
      inc si
      inc di
      loop s
      add bx,16
      pop cx
      loop s
      
      
      mov cx,21
      mov bx,0
 incom:mov ax,es:[si]
       mov [5+bx],ax
       add si,2
       mov ax,es:[si]
       mov [7+bx],ax
       add si,2
       add bx,16
       loop incom
       
       mov cx,21
       mov bx,0
 staff:mov ax,es:[si]
       mov [10+bx],ax
       add bx,16
       loop staff
       
       
       mov cx,21
       mov bx,0
   ave:mov ax,[bx+5]
       mov dx,[bx+7]
       div word ptr [bx+10]
       mov [13+bx],ax
       add bx,16
       loop ave
       
       
       mov ax,4c00h
       int 21h
        
       
       codesg ends
       end start

Chapter 9 principle of transfer instruction

1. Analyze the following program and think before running. Can this program return correctly?
Think again after running: why is this result?
Deepen the understanding of relevant contents through this procedure.

Can return correctly. The address of s2 is transferred to s through ax, and JMP short s actually jumps to s2. In s2, jmp short s1 calculates the offset between S1 and s2, and then adds the address of s to actually jump to the first address to complete the return

2. Programming: the string 'welcome to mask!' in green, red on green background and blue on white background will be displayed in the middle of the screen.

;In the middle of the screen, green, red, white and blue strings are displayed respectively'welcome to masm!'
assume ds:data, cs:code

data segment
db 'welcome to masm!'
data ends

code segment
start:  mov ax,data
        mov ds,ax
        mov ax,0b800h
        mov es,ax
        
        mov bx,0
        mov si,1664
        mov cx,16
   gree:mov al,ds:[bx]
        mov ah,10000010B
        mov es:[si],ax
        add si,2
        inc bx
        loop gree
        
        mov bx,0
        mov si,1824
        mov cx,16
    rad:mov al,ds:[bx]
        mov ah,10100100B
        mov es:[si],ax
        add si,2
        inc bx
        loop rad
        
        
        mov bx,0
        mov si,1984
        mov cx,16
   blue:mov al,ds:[bx]
        mov ah,11110001B
        mov es:[si],ax
        add si,2
        inc bx
        loop blue
     
        
        mov ax,4c00h
        int 21h
        code ends
        end start
        
        
        

Chapter 10 CALL and RET instructions

1. Display string

problem
Display string is a function often used in real work. A general subroutine should be written to realize this function
Yes. We should provide a flexible calling interface so that the caller can decide the display position (row, column, content and color).
Subroutine description
Name: show_str
Function: display a character string ending with mouth in the specified position and color,
Parameters: (dh) - line number (value range 0 ~ 24), (d = column number (value range 0 ~ 79),
(c) - color, ds:si points to the first address of the string
Return: None

Application example: in 8 rows and 3 columns of the screen, the string in the data section is displayed in green

; In 8 rows and 3 columns of the screen, the string in the data section is displayed in green

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

data segment  
    db 'welcome to masm!',0  
data ends  

stack segment  
    dw 8 dup (0)  
stack ends  

code segment  
start:mov ax,data
      mov ds,ax
      mov ax,stack
      mov ss,ax
      mov sp,16
      
      mov dh,8
      mov dl,3
      mov cl,2
      call show_str
      
      mov ax,4c00h
      int 21h
      
 show_str:push dx
          push cx
          push ax
          push ss
          push si
          
          mov ax,0b800h
          mov es,ax
          
;Calculate the line offset. The offset is saved in bx
   mov al,0a0h
   dec dh
   mul dh
   mov bx,ax
   
   mov al,2
   dec dl
   mul dl
   add bx,ax;
   
   mov di,0
   mov si,0
   mov al,cl
   
   s1:
   mov ch,0
   mov cl,ds:[di]
   jcxz ok
   mov ch,al
   mov es:[bx+si],cx
   
   add si,2
   inc di
   jmp short s1
   ok:
   pop si
   pop ss
   pop ax
   pop cx
   pop dx
   ret
   
code ends
end

2. Solve the problem of division overflow

Name: divdw
Function: perform division operation without overflow. The dividend is dword type, the divisor is word type, and the result is dword
Parameter: (AX) = lower 16 bits of DWORD data
(DX) = upper 16 bits of DWORD data
(cx) = divisor
Return: (dx) = the upper 16 bits of the result, (ax) - the lower 16 bits of the result
(cx) = remainder
Application example: calculate 1000000 / 10 (F4240H/0A)

assume cs:code, ss:stack

stack segment
dw 16 dup (0)
stack ends

code segment
start:  mov ax, stack
mov ss, ax
mov sp, 32
mov ax, 4240h
mov dx, 000fh
mov cx, 0ah
call divdw

mov ax, 4c00h
int 21h

3. Value display

Name: dtoc
Function: convert word data into a string representing decimal numbers, and the universal character string is represented by. Is the ending character.
Parameter: (ax)=word data
dssi points to the first address of the string
Return: None
Application example: programming, the data 12666 is listed in 8 rows of the screen in decimal form and displayed in green
Come on. When displaying, we call the first subroutine show str in this experiment.

assume cs:code, ss:stack

stack segment
dw 16 dup (0)
stack ends

data segment
db 10 dup (0)
data ends

code segment
start:  mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov sp, 32
mov ax, 12666
mov si, 0
call dtoc

mov dh, 8
mov dl, 3
mov cl, 2
call show_str

mov ax, 4c00h
int 21h

dtoc:   push ax
push si
push di
push dx
push bx
push cx
mov di, 0
mov dx, 0
mov bx, 10

devide: mov cx, ax
jcxz stop
div bx
inc di
push dx
mov dx, 0
jmp devide
stop:   mov cx, di
string: pop bx
add bx, 30h
mov [si], bl
inc si
loop string

pop cx
pop bx
pop dx
pop di
pop si
pop ax
ret

show_str:
push cx
push bx
push ax
push si
push di
push es
;using cx, bx, ax, si, di, es
mov ax, 0b800h
mov es, ax
mov bx, 0
mov di, 0
mov al, 160
mul dh
add bx, ax
mov al, 2
mul dl
add bx, ax ;bx stores address of start character
mov al, cl ;al stores the color of character
char:   mov ch, 0
mov cl, ds:[si]
jcxz zero
mov ch, al
mov es:[bx+di], cx
add di, 2
inc si
jmp char
zero:   pop es
pop di
pop si
pop ax
pop bx
pop cx
ret

code ends
end start

Chapter XI flag register

Write a subroutine to convert the lowercase letters in the string that contains any character and ends with 0 into large characters
Female, described below.
Name: letterc
Function: convert lowercase letters in strings ending with 0 into uppercase letters
Parameter: ds:si points to the first address of the string

assume cs:code

stack segment
dw 8 dup (0)
stack ends

data segment
db "Beginner's All-purpose Symbolic Instruction Code.", 0
data ends

code segment
begin:  mov ax, stack
        mov ss, ax
        mov sp, 16
        mov ax, data
        mov ds, ax
        mov si, 0
        call letterc

mov ax, 4c00h
int 21h

letterc: push cx
         push si
         pushf
         
         mov ch,0
       s:mov cl,ds:[si]
         jcxz ok
         cmp cl,97
         jb next
         cmp cl,122
         ja next
         sub cl,20h
         mov ds:[si],cl
    next:inc si
         jmp s
         
         
         
     ok: popf
         pop si
         pop cx
     ret

code ends
end begin

Chapter XII internal interruption

1. Write the program of interrupt 0, so that when the division overflow occurs, the string "divide error!" will be displayed in the middle of the screen, and then return to DOS.

Topics: server GNU