Advertisement
a3f

[8086 ASM] Simple I/O and summation

a3f
Jan 21st, 2014
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; given a string over std inpput calculate sum of multiplies of 5 smaller or equal of it
  2. ; eg: 34 gives => sum = 30 + 25 + 20 + 15 + 10 + 5 = 105
  3.  
  4. .data
  5. input DB ?
  6. sum DB ?
  7.  
  8. .code
  9.  
  10. org 100h
  11.  
  12. start:
  13. mov ah, 01h
  14. int 21h
  15. mov input, ah  
  16. mov si, input ;si points to string
  17.  
  18. loopStringToInt:
  19.  
  20.     lodsb                      ;load the first byte pointed by ESI in al
  21.  
  22.     cmp al,'0'                 ;check if it's an ascii number [0-9]
  23.     jb noascii                 ;not ascii, exit
  24.     cmp al,'9'                 ;check the if it's an ascii number [0-9]
  25.     ja noascii                 ;not ascii, exit
  26.  
  27.     sub al,30h                 ;ascii '0' = 30h, ascii '1' = 31h ...etc.
  28.     cbw                        ;byte to word
  29.     push ax
  30.     mov ax,bx                ;bx will contain '12345' in hexadecimal
  31.     mov cx,10
  32.     mul cx                    ;AX=AX*10
  33.     mov bx,ax
  34.     pop ax
  35.     add bx,ax
  36.     jmp loopStringToInt             ;continue until ESI points to a non-ascii [0-9] character
  37.     noascii:
  38.     ret                        ;bx contains integer now
  39.  
  40. loopFives:
  41. mov cx, 5 ; seed value = 5
  42. cmp bx, cx ; has cx reached bx yet?
  43. jna printsum ;if so print and pass operation back to OS
  44. add sum, cx ; if not, increase sum
  45. add cx, 5 ; get next multiply of 5
  46. jmp loopFives ;; continue loop
  47.  
  48. printsum:
  49.     mov dl, sum
  50.     mov ah, 02h
  51.     int 21h
  52. int 27h
  53. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement