Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. number db 7 dup 0 ; string which will store input and output
  2. n1 dw 0 ; two input variables
  3. n2 dw 0
  4. res dw 0 ; one output variable
  5. cr dw 13,10,"$" ; carriage return, line feed
  6. start:
  7. mov dx,offset number
  8. mov bx,dx
  9. mov b[bx],5 ; maximum 5 characters to read
  10. mov ah,0ah
  11. int 21h ; read in a string from keyboard
  12. mov bx,offset number +1
  13. mov cx,00
  14. mov cl,[bx] ; cl now contains number of digits
  15. mov ax,00 ; ax will contain the number input
  16. usedigit:
  17. inc bx ; get next digit
  18. shl ax,1 ; multiply by 10 using 2 shift ops and an add...
  19. mov dx,ax ; ... x*8 + x*2 = x*10 is the principle.
  20. shl ax,2
  21. add ax,dx ; ax is now multiplied by 10
  22. mov dx,00
  23. mov dl,[bx] ; dl has new character
  24. sub dx,48 ; subtract 48 = ascii('0') to get number value
  25. add ax,dx ; add to ax
  26. loop usedigit ; loop statement= jmp if cx > 0
  27. cmp n1,00 ; see if this is first or second number read
  28. jnz second
  29. mov n1,ax ; assign it to the first variable
  30. jmp start ; read in another number
  31. second:
  32. mov n2,ax ; or assign to second variable and continue
  33. print_cr:
  34. mov ah,09
  35. mov dx,offset cr ; print out a carriage return character
  36. int 21h
  37. addnos:
  38. mov ax,n1 ; move numbers to registers ...
  39. mov bx,n2
  40. add ax,bx ; ...and add
  41. mov res,ax ; store the result
  42. mov cx,00
  43. setup_string:
  44. mov bx,offset number+7 ; put a $ at end of buffer.
  45. mov b[bx],'$' ; we will fill buffer from back forwards
  46. dec bx
  47. mov ax,res
  48. convert_decimal:
  49. mov dx,10
  50. div dl ; divide by 10
  51. add ah,48 ; convert remainder to character
  52. mov [bx],ah ; and move to buffer for output
  53. dec bx
  54. mov ah,00 ; quotient becomes new value
  55. cmp ax,00 ; if we haven't got all digits divide again
  56. jnz convert_decimal
  57. printout:
  58. mov dx,bx
  59. inc dx ; we decremented once too many, go forward one.
  60. mov ah,09
  61. int 21h ; output the string
  62. close:
  63. mov ah,4ch
  64. mov al,00
  65. int 21h ; end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement