Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; lab.asm Program prints result of (Num^2) % Dig submitted by the client
- ;
- .MODEL SMALL
- .STACK 100h
- .DATA
- Num DW ?
- Dig DW ?
- Remainder DB ?
- Ten DW 10
- PromptStr DB 'Please Enter Number (from 00 up to 99):',13,10,'$'
- PromptStr2 DB 13,10,'Please Enter Digit (from 0 up to 9):',13,10,'$'
- ResultStr DB 13,10,'XX^2 mod X = ',13,10,'$'
- RoundUpStr DB 13,10,'The result should be round up',13,10,'$'
- RoundDownStr DB 13,10,'The result should be round down',13,10,'$'
- DivErrorStr DB 13,10,'Division error',13,10,'$'
- .CODE
- MOV AX,@DATA ; DS can be written to only through a register
- MOV DS,AX ; Set DS to point to data segment
- MOV AH,9 ; Set print option for int 21h
- MOV DX,OFFSET PromptStr ; Set DS:DX to point to PromptString
- INT 21h ; Print PromptString
- ; Read first digit of number
- MOV Num,0
- MOV AH,1
- INT 21h
- MOV ResultStr[2],AL
- SUB AL,'0'
- MOV AH,0
- MUL Ten
- MOV Num,AX
- ; Read second digit of number
- MOV AH,1
- INT 21h
- MOV ResultStr[3],AL
- SUB AL,'0'
- MOV AH,0
- ADD Num, AX
- ;
- MOV AH,9 ; Set print option for int 21h
- MOV DX,OFFSET PromptStr2 ; Set DS:DX to point to PromptString2
- INT 21h ; Print PromptString2
- ; Read Dig
- MOV Dig, 0
- MOV AH,1
- INT 21h
- MOV ResultStr[11],AL
- SUB AL,'0'
- MOV AH,0
- ADD Dig, AX
- ; Check Dig if ZERO
- CMP Dig, 0
- JE DivisionError
- ; Power Num by 2
- MOV AX,[Num]
- MUL [Num]
- ; Do Modulo by division
- DIV [Dig]
- ADD Remainder, DL
- ADD DL,'0'
- MOV ResultStr[15],DL ; Moves result to ResultStr
- ; Print Result of calculations
- MOV AH,9
- MOV DX,OFFSET ResultStr
- INT 21h
- ; Divides Dig by 2 and compares with Remainder
- MOV AX,[Dig]
- MOV BL,2
- DIV BL ; Divide Dig by 2
- CMP Remainder,AL ; Compare Remainder with Dig/2
- JGE RoundUp ; If greater/equal print RoundUp
- ; Otherwise, print RoundDown Message
- MOV AH,9
- MOV DX,OFFSET RoundDownStr
- JMP TerminateProg
- ;
- RoundUp:
- MOV AH,9
- MOV DX,OFFSET RoundUpStr
- JMP TerminateProg
- ; Print division by 0 error
- DivisionError:
- MOV AH,9
- MOV DX,OFFSET DivErrorStr
- ; Terminate program
- TerminateProg:
- INT 21h
- MOV AH,4Ch ; Set terminate option for int 21h
- INT 21h ; Return to DOS (terminate program)
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement