Advertisement
Il_Voza

2.3 Print decimal number

May 4th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Write a program that prints to the screen the decimal value of a positive integer number (0 ÷ 2^(16)-1), stored into an appropriate variable
  2.  
  3. ;IMPLEMENTATION
  4. ;Use an algorithm in two phases:
  5. ;-Decomposition of binary number into its digits by successive division for 10, saving the remainders and repeating operation on quotient until this is different from 0
  6. ;-Display of digits so obtained in reverse order than that initial, using the stack
  7. ;-N.B.: digits must be converted into ASCII characters before printing
  8.  
  9. ;I declare that about division I do it by
  10. ;a number in 32bit (DD) because when
  11. ;I do PUSH or POP, it wants needs
  12. ;registers in 16 bit, not in 8 bit, and so
  13. ;is easier
  14.  
  15. .MODEL small
  16. .STACK
  17. .DATA
  18. VAR DD 4325      ;numero da stampare
  19. divisore DW 10 ;divisore
  20. .CODE
  21. .START
  22. MOV AX,WORD PTR VAR
  23. MOV DX,WORD PTR VAR+2
  24. ciclo: DIV divisore ;This cycle converts my number
  25.        ADD DX,'0' ;I remember that in DIV DX stores the remainder.
  26.                   ;I sum +'0' so in DX I have the position of
  27.                   ;stored number but in ASCII code
  28.        PUSH DX    ;I store DX into STACK
  29.        MOV DX,0
  30.        INC BX     ;BX counts the digits (After I will need of this for printing cycle)
  31.        CMP AX,0   ;If quotient is 0, then go out from cycle
  32.        JNZ ciclo
  33.          
  34. MOV AH,2        ;OUTPUT
  35. stampa:  POP DX ;In DX (so in DL) will be stored ASCII code of digits that I stored into stack
  36.          INT 21h
  37.          DEC BX
  38.          JNZ stampa
  39. .EXIT
  40. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement