Advertisement
Sparkster

Untitled

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