Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .MODEL TINY
- .CODE
- MAIN PROC
- CALL INDEC
- PUSH AX
- CALL NEWLINE
- CALL NEWLINE
- POP AX
- CALL OUTDEC
- EXIT:
- MOV AH,4CH
- INT 21H
- MAIN ENDP
- CALL INDEC
- CALL OUTDEC
- NEWLINE PROC
- MOV AH,2
- MOV DL,10
- INT 21H
- MOV DL,13
- INT 21H
- RET
- NEWLINE ENDP
- OUTDEC PROC
- PUSH AX ;save registers
- PUSH BX
- PUSH CX
- PUSH DX
- @END_IF1:
- ;get decimal digits
- XOR CX,CX ;CX counts digits
- MOV BX,2D ;BX has divisor
- @REPEAT1:
- XOR DX,DX ;prepare high word of dividend
- DIV BX ;AX = quotient, DX = remainder
- PUSH DX ;save ramainder on stack
- INC CX ;count = count + 1
- ;until
- OR AX,AX ;quotient = 0?
- JNE @REPEAT1 ;no keep going
- ;converts digits to characters and print
- MOV AH,2 ;print char function
- ;for count times do
- @PRINT_LOOP:
- POP DX ;digit in DL
- OR DL,30H ;convert to character
- INT 21H ;print digit
- LOOP @PRINT_LOOP ;loop until done
- ;end_for
- POP DX ;restore registers
- POP CX
- POP BX
- POP AX
- RET
- OUTDEC ENDP
- INDEC PROC
- PUSH BX ;save registers used
- PUSH CX
- PUSH DX
- ;print prompt
- @BEGIN:
- MOV AH,2
- MOV DL,'?'
- INT 21H ;print '?'
- ;total=0
- XOR BX,BX ;BX holds total
- ;negative = false
- XOR CX,CX ;CX holds sign
- ;read a character
- MOV AH,1
- INT 21H ;character in AL
- ;end_case
- @REPEAT2:
- ;if character is between '0' and '9'
- CMP AL,'0' ;character >= '0'?
- JNGE @NOT_DIGIT ;illegal character
- CMP AL,'9' ;character <= '9'?
- JNLE @NOT_DIGIT ;no, illegal character
- ;then convert character to a digit
- AND AX,000FH ;convert to digit
- PUSH AX ;save on stack
- ;total = total * 10 + digit
- MOV AX,10 ;get 10
- MUL BX ;AX = total * 10
- POP BX ;retrieve digit
- ADD BX,AX ;total = total * 10 + digit
- ;read a character
- MOV AH,1
- INT 21H
- CMP AL,0DH ;carriage return?
- JNE @REPEAT2 ;no, keep going
- ;until CR
- MOV AX,BX ;store number in AX
- ;if negative
- OR CX,CX ;negative number
- JE @EXIT ;no, exit
- ;then
- NEG AX ;yes, negate
- ;end_if
- @EXIT:
- POP DX ;restore registers
- POP CX
- POP BX
- RET ;and return
- ;here if illegal character entered
- @NOT_DIGIT:
- MOV AH,2 ;move cursor to a new line
- MOV DL,0DH
- INT 21H
- MOV DL,0AH
- INT 21H
- JMP @BEGIN ;go to beginning
- INDEC ENDP
- END MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement