简体   繁体   中英

How to display the user's input in Emu8086 Assembly Language

I wanted to create a simple program in Emu8086 Assembly Language that will prompt the user for a value. Then it will display the user's value. After displaying the user's value, it will display a message. However, my problem is I couldn't display the user's value. Here's my code:

org 100h
            
.model small
            
.data

a db "Enter your favorite number; $"

b db "Your favorite number is $"     

c db "My favorite song is Blinding Lights by The Weeknd$"

.code

mov ax,@data
mov ds, ax
             
             
;Displaying a             
lea dx,a
mov ah,09h
int 21h

;User input
mov ah,01
int 21h                        
mov ah,01
int 21h                        

;Newline                     
mov dx,13
mov ah,2
int 21h  
mov dx,10
mov ah,2
int 21h
            
;Displaying b 
lea dx,b
mov ah,09h
int 21h    
 
;Displaying User's input 
mov dl,al
mov ah,2h
int 21h
mov dl,al
mov ah,2h
int 21h
        
;Newline                     
mov dx,13
mov ah,2
int 21h  
mov dx,10
mov ah,2
int 21h      
      
;Displaying c
lea dx,c
mov ah,09h
int 21h



ret

Michael already told you the problems with your code.
This is how you solve it.

  • The inputted characters that you get from the DOS.GetCharacter function 01h need to be stored somewhere. In this simple program you can use a couple of byte-sized registers like BL and BH .
  • To output a newline (carriage return 13 and linefeed 10), you can set these values in the register DL . There's no need to use the longer DX register here.
  • And since you should not repeat yourself, you should place that newline code in a subroutine that you can call when needed.
  • Those lea dx, SomeLabel instructions get a shorter encoding (from 4 bytes to 3 bytes) if you write mov dx, offset SomeLabel .
mov ax, @data
mov ds, ax

;Displaying a             
mov dx, offset a
mov ah, 09h
int 21h

;User input
mov ah, 01h
int 21h                    
MOV BH, AL
mov ah, 01h
int 21h                       
MOV BL, AL

call Newline
            
;Displaying b 
mov dx, offset b
mov ah, 09h
int 21h    
 
;Displaying User's input 
mov dl, BH
mov ah, 02h
int 21h
mov dl, BL
mov ah, 02h
int 21h

call Newline
      
;Displaying c
mov dx, offset c
mov ah, 09h
int 21h

ret

Newline:
  mov DL, 13
  mov ah, 02h
  int 21h  
  mov DL, 10
  mov ah, 02h
  int 21h
  ret

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