CSE 322 Spring 2021 Lab 7

      No Comments on CSE 322 Spring 2021 Lab 7

Task #1 (Program listing 9.2, 9.3, 9.4)

Decimal input output procedure

//Code
.MODEL SMALL
.STACK 100h
.DATA 
msg db 'The Value plus 10 is $'
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    call indec   
    mov bx, ax
    mov ah, 2
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h   
    mov ah, 9
    lea dx, msg
    int 21h
    add bx, 10
    mov ax, bx
    call outdec
    
exit:    
    mov ah, 4ch
    int 21h
main endp  

outdec proc  
    push ax
    push bx
    push cx
    push dx    
    
    or ax, ax
    jge endif1 
    
    ;Do these if number is negative  
    push ax
    mov ah, 2
    mov dl, '-'
    int 21h   
    pop ax
    neg ax    
endif1:
    ;Do these if number is positive or negative
    xor cx, cx  ;clear cx
    mov bx, 10
repeat1:
    xor dx, dx
    div bx
    push dx
    inc cx
    
    cmp ax, 0
    jne repeat1    
    
    ;Printing the numbers
    mov ah, 2            
printLoop:
    pop dx
    add dl, '0'
    int 21h
    loop printLoop    
    
    
    pop dx
    pop cx
    pop bx
    pop ax
    ret
outdec endp    

indec proc
    push bx
    push cx
    push dx   

begin2:    
    mov ah, 2
    mov dl, '?'
    int 21h
    
    ;clear registers
    mov bx, 0
    mov cx, 0
    
    ;Start taking inputs
    mov ah, 1
    int 21h         
    
    cmp al, '-'
    je minus
    cmp al, '+'
    je plus
    jmp repeat2
minus:
    mov cx, 1
plus:
    int 21h      
    
repeat2:
    cmp al, '0'
    jl notdigit
    cmp al, '9'
    jg notdigit
    
    and ax, 000fh    ;converting to digit. equivalent to sub al, '0'
    push ax
    
    mov ax, 10
    mul bx 
    pop bx 
    add bx, ax
    
    mov ah, 1
    int 21h
    cmp al, 0dh
    jne repeat2
    
    mov ax, bx  
    
    cmp cx, 0
    je exitproc
    neg ax
    jmp exitproc

notdigit: 
    mov ah, 2
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h
    jmp begin2  

exitproc:    
    pop dx
    pop cx
    pop bx
    ret
indec endp    

end main

Task #2

Write a procedure to find the factorial of a number stored in cx.

//Code
.MODEL SMALL
.STACK 100h
.DATA 
msg db 'The Factorial is $'
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    call indec   
    mov cx, ax
    mov ah, 2
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h   
    mov ah, 9
    lea dx, msg
    int 21h
    call factorial  
    call outdec 
    
exit:    
    mov ah, 4ch
    int 21h
main endp     

;n! n is stored in cx. n!is provided in ax. ax=cx!
factorial proc
    push cx
    mov ax, 1

label1:    
    mul cx
    loop label1
    pop cx
    ret
factorial endp

outdec proc  
    push ax
    push bx
    push cx
    push dx    
    
    or ax, ax
    jge endif1 
    
    ;Do these if number is negative  
    push ax
    mov ah, 2
    mov dl, '-'
    int 21h   
    pop ax
    neg ax    
endif1:
    ;Do these if number is positive or negative
    xor cx, cx  ;clear cx
    mov bx, 10
repeat1:
    xor dx, dx
    div bx
    push dx
    inc cx
    
    cmp ax, 0
    jne repeat1    
    
    ;Printing the numbers
    mov ah, 2            
printLoop:
    pop dx
    add dl, '0'
    int 21h
    loop printLoop    
    
    
    pop dx
    pop cx
    pop bx
    pop ax
    ret
outdec endp    

indec proc
    push bx
    push cx
    push dx   

begin2:    
    mov ah, 2
    mov dl, '?'
    int 21h
    
    ;clear registers
    mov bx, 0
    mov cx, 0
    
    ;Start taking inputs
    mov ah, 1
    int 21h         
    
    cmp al, '-'
    je minus
    cmp al, '+'
    je plus
    jmp repeat2
minus:
    mov cx, 1
plus:
    int 21h      
    
repeat2:
    cmp al, '0'
    jl notdigit
    cmp al, '9'
    jg notdigit
    
    and ax, 000fh    ;converting to digit. equivalent to sub al, '0'
    push ax
    
    mov ax, 10
    mul bx 
    pop bx 
    add bx, ax
    
    mov ah, 1
    int 21h
    cmp al, 0dh
    jne repeat2
    
    mov ax, bx  
    
    cmp cx, 0
    je exitproc
    neg ax
    jmp exitproc

notdigit: 
    mov ah, 2
    mov dl, 0dh
    int 21h
    mov dl, 0ah
    int 21h
    jmp begin2  

exitproc:    
    pop dx
    pop cx
    pop bx
    ret
indec endp    

end main

Task #3 (Example 10.3)

Sum of 10 elements in array

//Code
.MODEL SMALL
.STACK 100h
.DATA 
w dw 10,20,30,40,50,60,70,80,90,100
x dw 100 dup(0)
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    mov ax, 0
    lea si, w   
    
    mov cx, 10
label1:
    add ax, [si]
    add si, 2
    loop label1 
  
exit:    
    mov ah, 4ch
    int 21h
main endp   
end main

Task #4

Sum of elements in array ending with $

//Code
.MODEL SMALL
.STACK 100h
.DATA 
w dw 10,20,30,40,50,'$'
x dw 100 dup(0)
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    mov ax, 0
    lea si, w   
    
label1:        
    cmp [si], '$'
    je end_
    add ax, [si]
    add si, 2
    jmp label1 
end_:  

exit:    
    mov ah, 4ch
    int 21h
main endp   
end main

Task #5 (Example 10.4, program listing 10.1)

Array (Each element is 16 bit) Reverse Procedure

//Code
.MODEL SMALL
.STACK 100h
.DATA 
w dw 10,20,30,40, 50, 60
x dw 100 dup(0)
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    lea si, w  
    mov bx, 6
    call reverse 
    

exit:    
    mov ah, 4ch
    int 21h
main endp  
;si contains offset of the array
;bx contains number of elements in the array
reverse proc
    push ax
    push bx
    push cx
    push si
    push di  
    
    mov di, si
    mov cx, bx
    
    dec bx
    shl bx, 1
    add di, bx
    shr cx, 1

exchangeloop:  
    mov ax, [si]
    xchg ax, [di]
    mov [si], ax
    add si, 2
    sub di, 2
    loop exchangeloop
        
    
    pop di
    pop si
    pop cx
    pop bx
    pop ax
    ret
reverse endp    

end main

Task #6

Two dimensional array usage.
Finding the average of different tests

//Code
.MODEL SMALL
.STACK 100h
.DATA  
five dw 5
scores  dw 67, 45, 98, 33
        dw 70, 56, 87, 44 
        dw 82, 72, 89, 40 
        dw 80, 67, 95, 50 
        dw 78, 76, 92, 60 
average dw 5 dup(0)
.CODE
main proc
    mov ax, @data
    mov ds, ax    
    
    mov si, 6
repeat:
    mov cx, 5
    mov bx, 0
    mov ax, 0

for:
   add ax, scores[bx,si]
   add bx, 8
   loop for 
   
   mov dx, 0
   div five        
   mov average[si], ax  
   sub si, 2
   jnl repeat 
   
   mov bx, average[6]
    

exit:    
    mov ah, 4ch
    int 21h
main endp  
 

end main

Homework

  1. Take 2 decimal numbers greater than 10 as inputs and print their sum and difference afterward
    Sample execution
    ?120
    ?15
    Sum is 135
    Difference is 105
  2. Add hex input output and binary input output procedure to the decimal input out put procedure and do the following tasks.
    a. put the last 5 digit of your registration number in ax (Treat the number as decimal) and print their equivalent binary and hexadecimal number.
    b. put the last 5 digit of your registration number in ax (Treat the number as hexadecimal) and print their equivalent binary and decimal number.
    c. put 100010101010 in ax and print the equivalent decimal and hexadecimal number.
    d. take hexadecimal input, binary input, decimal input
  3.  Chapter 8 problem 8
  4.  Chapter 8 problem 10
  5.  Change the code in task 5 to work for array of 8-bit elements, which can be used to reverse string.
  6. Section 10.3: Application of sorting. Program listing 10.2,10.3
  7. Change the code in task 6 to find the average test score of each students.

 

Leave a Reply

Your email address will not be published. Required fields are marked *