Advertisement
AVONnadozie

PROCEDURE TO COLLECT MULTIPLE DIGITS AS INPUT IN 8086 ASM

Mar 4th, 2013
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;PROCEDURE TO COLLECT MULTIPLE DIGITS AS INPUT IN 8086 ASSEMBLY
  2. ;AUTHOR: ANUEBUNWA VICTOR O.
  3. ;ASSEMBLER: MASM611
  4.  
  5. .MODEL SMALL
  6. .STACK
  7. .CODE
  8. value DB 0000H
  9. e1 DB "Wrong input",'$'
  10. e2 DB 'No value entered',0AH,0DH,'Program exiting...','$'
  11.  
  12. .CODE
  13. START:
  14. MOV AX, @data
  15. MOV DS, AX
  16. ;YOUR CODES GOES HERE
  17. MOV AH, 4CH
  18. INT 21H
  19.  
  20. ;You may avoid using PUSHA and POPA if it gives error during assembling but be sure to backup needed ;values in AX,BX,CX and DX before calling this procedure.
  21.  
  22. ;Beginning of procedure
  23. read            proc                       
  24.                 PUSHA                   ;Push current values in register to stack
  25.                 MOV AX, 0000H       ;
  26.                 MOV CX, 0000H       ;
  27.                 MOV BX, 0000H       ;
  28.                 MOV DX, 0000H       ;Clears register AX,BX,CX and DX           
  29. .tryloop1:              MOV AH, 01H
  30.                 INT 21H             ;Calls DOS funtion to receive a character
  31.                 JMP check           ;Jumps to check character recieved
  32. continue_loop:                  ;Where loop continues assuming character passes check
  33.                 MOV BX, 0000H       ;Clears BX again
  34.                 MOV BL, AL          ;Moves AL to BL
  35.                 SUB BX, 30H         ;Subtracts 30H to get the actual value of character
  36.                 PUSH BX         ;Push BX to stack
  37.                 INC CX              ;Increments CX to track the lenght of the value
  38.                 CMP AL, 0DH     ;Checks if character was carriage return
  39.                 JNE .tryloop1           ;Jumps if not carrige return               
  40.                
  41.                 CMP CX, 1           ;Checks if no value was entered
  42.                 JE error_msg2           ;jumps if no value was entered
  43.                 POP AX          ;Pops out last value in stack(carrige return)
  44.                 dec CX              ;Decrements counter
  45.                
  46.                 MOV BX, 1           ;initialize multiplier
  47. .tryloop2:              MOV AX, 0000H       ;Clears AX
  48.                 POP AX              ;Pops data to AX
  49.                 MUL BX              ;Multiplies AX by BX
  50.                 ADD value, AX       ;Add AX to value
  51.                
  52.                 XCHG AX, BX     ;Exchanges AX and BX
  53.                 MOV BX, 10          ;Moves 10 into BX
  54.                 MUL BX              ;Multiplies AX by BX(containing 10)
  55.                 XCHG AX, BX     ;Exchanges back AX and BX
  56.                 dec CX              ;Decrements counter
  57.                 JNE .tryloop2           ;Repeats until CX equals zero
  58.            
  59.                 MOV AH, 02H
  60.                 MOV DL, 0aH
  61.                 INT 21H             ;Prints line-feed
  62.                 POPA              ;Pops values of register before procedure call from stack
  63.                 RET                 ;returns control
  64. read        endp                                             ;End of procedure.
  65.  
  66. check:          CMP AL, 0DH     ;Checks if the character is carriage return
  67.                 JE continue_loop        ;Jumps back into loop if carriage return
  68.                 CMP AL, '0'         ;Checks if a non-digit was entered
  69.                 JNGE error_msg
  70.                 CMP AL, '9'
  71.                 JNLE error_msg      ;Displays error if a non-digit was entered
  72.                 JMP continue_loop       ;Else it jumps back into loop
  73.  
  74. error_msg:      MOV AH, 02H
  75.                 MOV DL, 0AH
  76.                 INT 21H             ;Prints line-feed
  77.                 LEA DX, e1
  78.                 MOV AH, 09H
  79.                 INT 21H             ;Prints string
  80.                 JMP start ;Restarts program(assuming you have Start: as your starting label)
  81. END START
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement