Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; lab.asm Program prints result of (Num^2) % Dig submitted by the client
  2. ;
  3.     .MODEL SMALL
  4.     .STACK 100h
  5.     .DATA
  6. Num          DW ?
  7. Dig          DW ?
  8. Remainder    DB ?
  9. Ten          DW 10
  10. PromptStr    DB 'Please Enter Number (from 00 up to 99):',13,10,'$'
  11. PromptStr2   DB 13,10,'Please Enter Digit (from 0 up to 9):',13,10,'$'
  12. ResultStr    DB 13,10,'XX^2 mod X = ',13,10,'$'
  13. RoundUpStr   DB 13,10,'The result should be round up',13,10,'$'
  14. RoundDownStr DB 13,10,'The result should be round down',13,10,'$'
  15. DivErrorStr  DB 13,10,'Division error',13,10,'$'
  16.  
  17.     .CODE
  18.     MOV AX,@DATA             ; DS can be written to only through a register
  19.     MOV DS,AX                ; Set DS to point to data segment 
  20.     MOV AH,9                 ; Set print option for int 21h
  21.     MOV DX,OFFSET PromptStr  ;  Set  DS:DX to point to PromptString
  22.     INT 21h                  ;  Print PromptString 
  23. ;       Read first digit of number
  24.     MOV Num,0
  25.     MOV AH,1
  26.     INT 21h
  27.     MOV ResultStr[2],AL
  28.     SUB AL,'0'
  29.     MOV AH,0
  30.     MUL Ten
  31.     MOV Num,AX
  32. ;       Read second digit of number
  33.     MOV AH,1
  34.     INT 21h
  35.     MOV ResultStr[3],AL
  36.     SUB AL,'0'
  37.     MOV AH,0
  38.     ADD Num, AX
  39. ;
  40.     MOV AH,9                  ;  Set print option for int 21h
  41.     MOV DX,OFFSET PromptStr2  ;  Set  DS:DX to point to PromptString2
  42.     INT 21h                   ;  Print PromptString2   
  43. ;       Read Dig
  44.     MOV Dig, 0
  45.     MOV AH,1
  46.     INT 21h
  47.     MOV ResultStr[11],AL
  48.     SUB AL,'0'
  49.     MOV AH,0
  50.     ADD Dig, AX
  51. ;       Check Dig if ZERO
  52.     CMP Dig, 0
  53.     JE  DivisionError
  54. ;       Power Num by 2
  55.     MOV AX,[Num]
  56.     MUL [Num]
  57. ;       Do Modulo by division
  58.     DIV [Dig]
  59.     ADD Remainder, DL
  60.     ADD DL,'0'
  61.     MOV ResultStr[15],DL       ; Moves result to ResultStr
  62. ;       Print Result of calculations
  63.     MOV AH,9
  64.     MOV DX,OFFSET ResultStr
  65.     INT 21h
  66. ;       Divides Dig by 2 and compares with Remainder
  67.     MOV AX,[Dig]
  68.     MOV BL,2
  69.     DIV BL                      ; Divide Dig by 2
  70.     CMP Remainder,AL            ; Compare Remainder with Dig/2
  71.     JGE RoundUp                 ; If greater/equal print RoundUp
  72. ;       Otherwise, print RoundDown Message
  73.     MOV AH,9
  74.     MOV DX,OFFSET RoundDownStr
  75.     JMP TerminateProg
  76. ;
  77. RoundUp:
  78.     MOV AH,9
  79.     MOV DX,OFFSET RoundUpStr
  80.     JMP TerminateProg
  81.    
  82. ;       Print division by 0 error
  83. DivisionError:
  84.     MOV AH,9
  85.     MOV DX,OFFSET DivErrorStr
  86. ;       Terminate program
  87. TerminateProg:
  88.     INT 21h
  89.     MOV AH,4Ch                  ; Set terminate option for int 21h
  90.     INT 21h                     ; Return to DOS (terminate program)
  91.     END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement