简体   繁体   中英

how can i input 2 digit number in assembly emu8086

mov ah,01h
int 21h

I know this is how you enter a one digit number or a char, but how can i enter for example the number 38. Thank you

This should work for you:

.model small
.data
.code

mov ax, @data
mov ds, ax 

mov dl, 10  
mov bl, 0         

scanNum:

      mov ah, 01h
      int 21h

      cmp al, 13   ; Check if user pressed ENTER KEY
      je  exit 

      mov ah, 0  
      sub al, 48   ; ASCII to DECIMAL

      mov cl, al
      mov al, bl   ; Store the previous value in AL

      mul dl       ; multiply the previous value with 10

      add al, cl   ; previous value + new value ( after previous value is multiplyed with 10 )
      mov bl, al

      jmp scanNum    

exit:

      mov ah, 04ch   
      int 21h

 end
.model small      
.stack 100h     
.data      
.code          
main proc         
mov ah, 1    
int 21h     
mov bl,al    
mov al, 1    
int 21h     
add bl,al    
sub bl, 48    
mov dl,bl    
mov ah, 2    
int 21h    
mov ah, 4ch    
int 21h    
main endp    
end main    
.MODEL SMALL
 .STACK 100H

 .DATA
    PROMPT_1  DB  'Enter the First digit : $'
    PROMPT_2  DB  'Enter the Second digit : $'
    PROMPT_3  DB  'Sum of First and Second digit : $'

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX

     LEA DX, PROMPT_1             ; load and display the PROMPT_1
     MOV AH, 9
     INT 21H

     MOV AH, 1                    ; read a character\\digit
     INT 21H

     MOV BL, AL                   ; save First digit in BL in ASCII code
     SUB BL, 30H

     MOV AH, 2                    ; carriage return
     MOV DL, 0DH
     INT 21H

     MOV DL, 0AH                  ; line feed
     INT 21H

     LEA DX, PROMPT_2             ; load and display the PROMPT_2
     MOV AH, 9
     INT 21H

     MOV AH, 1                    ; read a character
     INT 21H

     MOV BH, AL                   ; save Second digit in BH in ASCII CODE
     SUB BH, 30H

     MOV AH, 2                    ; carriage return
     MOV DL, 0DH
     INT 21H

     MOV DL, 0AH                  ; line feed
     INT 21H

     LEA DX, PROMPT_3             ; load and display the PROMPT_3
     MOV AH, 9
     INT 21H

     ADD BL, BH                   ; add First and Second digit
     ADD BL, 30H                  ; convert ASCII to DECIMAL code

     MOV AH, 2                    ; display the character
     MOV DL, BL     
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM