Advertisement
Guest User

Untitled

a guest
May 29th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Compute n^3 + n^2 +9
  2.  
  3. data segment para public 'data'
  4. mess1 DB "Introduce the number n: $"
  5. nr DB 2,?,2 DUP(?)
  6. number DB ?
  7. result DB ?
  8. theResult DB 2 DUP(?),'$'
  9. mess2 DB 0Ah,0Dh,"The result is: $"
  10. data ends
  11.  
  12. print macro message
  13.         mov ah,09h
  14.         mov dx,offset message
  15.         int 21h
  16. endm
  17.  
  18. read macro nr
  19.         mov ah,0Ah
  20.         mov dx,offset nr
  21.         int 21h
  22. endm
  23.  
  24. ;compute from string to number
  25. convert macro nr,number
  26. LOCAL change
  27.  
  28.     mov dx,0
  29.     mov si,2
  30.     mov ax,0
  31.     mul bl
  32.     mov ch,0
  33.     mov cl,nr[1]
  34.  
  35.    
  36.     change: mul bl
  37.             mov al,nr[si]
  38.             sub al,'0'
  39.             dec si
  40.             loop change
  41.  
  42.    
  43. endm
  44.  
  45. code segment para public 'code'
  46. assume cs:code, ds:data
  47. start proc far
  48.         push ds
  49.         mov ax,0
  50.         push ax
  51.        
  52.         mov ax,data
  53.         mov ds,ax
  54.        
  55.         ;print the message
  56.         print mess1
  57.        
  58.         ;read the number
  59.         read nr
  60.        
  61.         ;convert the number to a real number
  62.         convert nr,number
  63.        
  64.        ; compute the sum in dx
  65.        mov dx,0
  66.        mov number,al
  67.        mov bl,number
  68.        
  69.        mul bl      ; al*number
  70.        add dx,ax
  71.        mul bl      ;al*number*number
  72.        add dx,ax    
  73.        add dx,9
  74.        mov result,dl
  75.        
  76.        ;make result printable
  77.        
  78.        mov al,result
  79.        mov ah,0
  80.        mov di,1   ; for theResult
  81.        mov bl,10
  82.        
  83.        loop1:  div bl
  84.                mov theResult[di],ah    ;ah stores the quotient
  85.                add theResult[di],'0'
  86.                dec di
  87.                mov ah,0                 ; for the next iteration
  88.                cmp al,0
  89.                jne loop1
  90.        
  91.         ;print a message
  92.         print mess2
  93.        
  94.         ;print the result
  95.         print theResult
  96.        
  97. ret
  98. start endp
  99. code ends
  100. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement