Advertisement
Il_Voza

2.4 Storage of a numeric value with more digits

May 4th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;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
  2. ;-Deepening: Acquire 5 positive integers separated by ENTER and store them into a word vector
  3.  
  4. ;IMPLEMENTATION
  5. ;Use an algorithm in two phases:
  6. ;-In first obtain ASCII characters
  7. ;-In second, they are converted in integer, evaluating possible presence of overflow
  8.  
  9. ;The two phases can be developed into same cycle
  10.  
  11. ;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
  12.  
  13. .MODEL small
  14. .STACK
  15. .DATA
  16. VAR DW ?
  17. DIM DW ?
  18. STR DW 'Overflow Error'
  19. .CODE
  20. .START
  21.  
  22. MOV DI,0
  23. MOV CX,10
  24. ciclo: MOV AH,1
  25.        INT 21h
  26.        CMP AL,13; ASCII code of ENTER
  27.        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
  28.        SUB AL,'0' ; So it prints ASCII character of inserted number
  29.        MOV AH,0  
  30.        PUSH AX   ;I store into stack the values taken from keyboard
  31.        INC DI
  32.        CMP DI,5
  33.        JNE ciclo
  34.        
  35. cycle0:      
  36. MOV DIM,DI   ;I must use DIM because if I have a number with less than 5 digits, it gives me problems    
  37.  
  38. cycle1: CMP DI,0   ;DI initially will be = DIM. But when I arrive to 0, it means that I finished
  39.         JE esci
  40.         DEC DI
  41.         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
  42.         DEC BX
  43.         SUB BX,DI
  44.         POP AX   ;I take last value stored into stack and put it into AX
  45.        
  46.  
  47.  
  48. cycle2: CMP BX,0  ;Descr to below below
  49.         JE aggiungi
  50.         DEC BX
  51.         MUL CX
  52.         JO esciERR ;Overflow da 70000 (su 16bit)
  53.         JMP cycle2
  54.        
  55.  
  56. aggiungi: ADD VAR,AX
  57.           JC esciERR ;Overflow da 65535 a 69999
  58.           JMP cycle1
  59.  
  60.  ;Here there is the error handling
  61.  MOV SI,0      
  62.  esciERR:
  63.  MOV AH,2
  64.  MOV DX,STR[SI]
  65.  INT 21h
  66.  INC SI
  67.  CMP SI,15
  68.  JNE esciERR
  69.  esci:
  70. .EXIT
  71. END
  72.  
  73. ;The strategy of code is:
  74. ;In ciclo I store numbers entered from keyboard
  75. ;somewhere, into a vector or into stack
  76. ;like I did.
  77. ;In cycle1 what is BX?
  78. ;Let's suppose to have read from keyboard 12345:
  79. ;Since keyboard reads a digits at the time
  80. ;I stored all into stack
  81. ;I think my number 12345 as separated in
  82. ;units(5),tens(4),hundreds(3) etc...
  83. ;So that I can store a number with more digits
  84. ;into a variable, first of all BX will be carried to 1, then through MUL CX I multiply
  85. ; tens for 10, so 4*10 (BX meanwhile will be decremented to 0), and I sum this to units(5)
  86. ;so I have 45. Then, about hundreds, BX will be carried to 2, so everytime that I decrease BX,
  87. ;I will have a MUL CX, so, since BX must go down to 0, BX will be decremented 2 times,
  88. ;so the digit stored into AL (that is 3) will be multiplied 2 times for 10,
  89. ;so I get 300, that will be added at value that is in VAR, that is 45, and I get 345.
  90. ;And so on for the remaining digits
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement