Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Write a program that ask the user a positive integer (possibly composed by more digits, and concluded with ENTER) and saves it into a word variable. The insert of huge values must report an error
- ;-Deepening: Acquire 5 positive integers separated by ENTER and store them into a word vector
- ;IMPLEMENTATION
- ;Use an algorithm in two phases:
- ;-In first obtain ASCII characters
- ;-In second, they are converted in integer, evaluating possible presence of overflow
- ;The two phases can be developed into same cycle
- ;NB: input from keyboard takes only one number at a time. In this ex I try to store a number with more digits into a variable
- .MODEL small
- .STACK
- .DATA
- VAR DW ?
- DIM DW ?
- STR DW 'Overflow Error'
- .CODE
- .START
- MOV DI,0
- MOV CX,10
- ciclo: MOV AH,1
- INT 21h
- CMP AL,13; ASCII code of ENTER
- JE cycle0 ; If AL=ENTER, JE does to jump it to cycle0. I do to jump there, so it stores dimension in DIM, otherwise it doesn't
- SUB AL,'0' ; So it prints ASCII character of inserted number
- MOV AH,0
- PUSH AX ;I store into stack the values taken from keyboard
- INC DI
- CMP DI,5
- JNE ciclo
- cycle0:
- MOV DIM,DI ;I must use DIM because if I have a number with less than 5 digits, it gives me problems
- cycle1: CMP DI,0 ;DI initially will be = DIM. But when I arrive to 0, it means that I finished
- JE esci
- DEC DI
- MOV BX,DIM ;Here I do BX=DIM-1-DI, i.e., since DI starts from DIM, BX initially will be 0 and slowly grows
- DEC BX
- SUB BX,DI
- POP AX ;I take last value stored into stack and put it into AX
- cycle2: CMP BX,0 ;Descr to below below
- JE aggiungi
- DEC BX
- MUL CX
- JO esciERR ;Overflow da 70000 (su 16bit)
- JMP cycle2
- aggiungi: ADD VAR,AX
- JC esciERR ;Overflow da 65535 a 69999
- JMP cycle1
- ;Here there is the error handling
- MOV SI,0
- esciERR:
- MOV AH,2
- MOV DX,STR[SI]
- INT 21h
- INC SI
- CMP SI,15
- JNE esciERR
- esci:
- .EXIT
- END
- ;The strategy of code is:
- ;In ciclo I store numbers entered from keyboard
- ;somewhere, into a vector or into stack
- ;like I did.
- ;In cycle1 what is BX?
- ;Let's suppose to have read from keyboard 12345:
- ;Since keyboard reads a digits at the time
- ;I stored all into stack
- ;I think my number 12345 as separated in
- ;units(5),tens(4),hundreds(3) etc...
- ;So that I can store a number with more digits
- ;into a variable, first of all BX will be carried to 1, then through MUL CX I multiply
- ; tens for 10, so 4*10 (BX meanwhile will be decremented to 0), and I sum this to units(5)
- ;so I have 45. Then, about hundreds, BX will be carried to 2, so everytime that I decrease BX,
- ;I will have a MUL CX, so, since BX must go down to 0, BX will be decremented 2 times,
- ;so the digit stored into AL (that is 3) will be multiplied 2 times for 10,
- ;so I get 300, that will be added at value that is in VAR, that is 45, and I get 345.
- ;And so on for the remaining digits
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement