CSE 322 Spring 2021 Lab 3

      No Comments on CSE 322 Spring 2021 Lab 3

Task #1

A basic while loop

//Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 
    
label1:
    mov ah, 1
    int 21h
    mov dl, al   
    sub dl, 20h
    mov ah, 2
    int 21h   
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h  
    
    jmp label1     ;Code moves to label 1
    
    ;Exit code
    mov ah, 4ch
    int 21h
    
    
endp MAIN

end MAIN

Task #2 (Example 6.1) [If-else structure]

suppose AX and BX contain signed numbers. Write some code to put the biggest one in cx.

//Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 
    
    mov bx, 50h
    mov ax, 55h
    
    cmp ax, bx
    jge label1
    jl label2    
    
label1:
    mov cx, ax  
    jmp exit
Label2:
    mov cx, bx      
    
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN   

Task #3 (Example 6.2) [If-then structure]

Replace the number in AX by its absolute value.

//Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 
    
    mov ax, 10
    cmp ax, 0
    jg exit ;If positive do nothing and exit
    
    ;if not positive negate the number
    neg ax         
    
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN   

Task #4 (Example 6.3)

Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence.

//Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 
    
    mov ah, 1
    int 21h      
    mov bl, al
    int 21h
    mov bh, al  ;After this line we have 2 ASCII codes stored in BL and BH        
    mov ah, 2   
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h
    
    cmp bl, bh
    jl print1
    jg print2

print1:
    mov dl, bl
    int 21h   
    jmp exit:

print2:  
    mov dl, bh
    int 21h          
    
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN

//Alternative Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 
    
    mov ah, 1
    int 21h      
    mov bl, al
    int 21h
    mov bh, al  ;After this line we have 2 ASCII codes stored in BL and BH        
    mov ah, 2   
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h
    
    cmp bl, bh
    jg print
    
    ;Print if less
    mov dl, bl
    int 21h   
    jmp exit:

print:  
    mov dl, bh
    int 21h          
    
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN   

Task #5 (Example 6.4) [Case structure]

If AX contains a negative number, put -1 In BX; if AX contains 0, put 0 In BX; if AX contains a positive number, put 1 In BX.

//Code
.MODEL small
.STACK 100H
.DATA  
a db '**********',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 

;ax=+ve, bx=1
;ax=-ve, bx=-1
;ax=0, bx=0
    
    mov ax, 500
    
    cmp ax, 0
    je equal
    jl less
    jg greater

equal:  
    mov bx, 0  
    jmp exit

less:        
    mov bx, -1 
    jmp exit

greater:      
    mov bx, 1    
        
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN

Task #6 (Example 6.5) [Another Case structure]

If AL contains 1 or 3, display “o”; if AL contains 2 or 4,display “e”.

//Code
.MODEL small
.STACK 100H
.DATA  
o db 'Number is odd',0dh,0ah,'$'   
e db 'Number is even',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 

    mov al, 2
    
    cmp al, 1
    je odd
    cmp al, 2
    je even
    cmp al, 3
    je odd
    cmp al, 4
    je even

odd:
    mov ah, 9
    lea dx, o
    int 21h
    jmp exit
even: 
    mov ah, 9
    lea dx, e
    int 21h     
        
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN

Task #7

Take a number from 0-9 as input and print if it is odd or even.

//Code
.MODEL small
.STACK 100H
.DATA  
o db 'Number is odd',0dh,0ah,'$'   
e db 'Number is even',0dh,0ah,'$'       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 

    mov ah, 1
    int 21h   
    sub al, '0'
    
    cmp al, 1
    je odd
    cmp al, 2
    je even
    cmp al, 3
    je odd
    cmp al, 4
    je even 
    cmp al, 5
    je odd
    cmp al, 6
    je even
    cmp al, 7
    je odd
    cmp al, 8
    je even
    cmp al, 9
    je odd
    cmp al, 0
    je even

odd:
    mov ah, 9
    lea dx, o
    int 21h
    jmp exit
even: 
    mov ah, 9
    lea dx, e
    int 21h     
        
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN

Task #8 (Example 6.8)

Write a count-controlled loop to display a row of 80 stars

//code
.MODEL small
.STACK 100H
.DATA       
.CODE
MAIN Proc
    mov ax, @data
    mov ds, ax 

    mov cx, 80
    
    mov ah, 2
    mov dl, '*'
label1:
    int 21h
    loop label1    
    
        
exit:
    ;Exit code
    mov ah, 4ch
    int 21h       
    
endp MAIN
end MAIN

Task #9

Draw a 10*10 box of stars without using any string. Use only flow control instructions and loop to draw the box.

Sample Execution

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
//Code
//To be done by students
Print Friendly, PDF & Email

Leave a Reply