3

I'm new to assembly language and I am having trouble with my code. At first I tried 1 input and then 1 output and it works just fine. But when I try 2 inputs. That's when the problem shows up. When it asks "Gender" input 1 and output 1 seems to overlap it.

I have searched thoroughly and managed to find one that asks the same thing but his/hers was different and I can't seem to understand. I hope someone can help. This is for school.

Full Code:

.model small
.stack 200
.data
    message db "Name: ","$"
    message2 db "Your name is: ","$"
    message3 db "Gender: ","$"
    message4 db "Your name is: ","$"
    BUF DB 100  
    DB 100 DUP("$") 

.code
MOV ax,@data
mov ds,ax

LEA dx,message  
mov ah,09h
int 21h
mov ah,0ah ;
mov dx, offset buf
int 21h

LEA dx,message2 
mov ah,09h
int 21h
LEA DX,BUF  
ADD DX,02   
MOV AH,09H
INT 21H

LEA dx,message3 
mov ah,09h
int 21h
mov ah,0ah 
mov dx, offset buf
int 21h


LEA dx,message4 
mov ah,09h
int 21h
LEA DX,BUF  
ADD DX,02   
MOV AH,09H
INT 21H

MOV AX,4C00H
INT 21H
END
eLjA
  • 31
  • 6

2 Answers2

3

You should write the terminating character '$' explicitly after each AH=0Ah call (or use separate buffers). Otherwise if you first type string '123456' and then 'abc', the buffer will hold first 123456\n$$$$$$... and abc\n56\n$$$$$ the next time.

The interrupt AH=0Ah will place the number of characters read at position DX + 1. For some reason the input interrupt uses '\n' as the terminating character, while AH=09h / print string uses '$'.

 ;; given dx == offset buf, replace the Carriage Return
 ;; with the terminating character '$'
 fix_end_of_string:
 mov bx, dx
 mov al, [bx + 1]
 mov ah, 0
 add bx, ax
 mov al, '$'
 mov [bx + 2], al
 ret

Some random reference to AH=0Ah / AH=09h services

The other problem, as indicated in other answer, is how DOS manages the terminal as a simulated typewriter with separate carriage return and line feed. The CR (0x0d) only sets the "writing head" to the beginning of line, but does not advance to the next line. In DOS this is done by writing Line Feed (ASCII 0x0a), and it's completely independent of the CR. One can move to next line at any horizontal position and continue typing. When one enters a name, apparently the Input service AH=0Ah will internally type both CR and LF allowing the next message to be printed on separate line, but the input buffer will only have the CR.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
2

All the printing is done on the same line. In order to advance to the next line you need to print \n\r but of course not like that as those are interpreted by the high-level languages. You need to use their ASCII codes: 13,10. So extend your code (.data section) with this:

newline db 13,10,"$"

and wherever you want to go to the new line just print that:

 lea dx, newline
 mov ah, 09h
 int 21h

enter image description here

Also message4 should be "Your gender is: ","$"

Paweł Łukasik
  • 3,893
  • 1
  • 24
  • 36
  • I added the "newline db 13,10,"$" " below .data But I seem to get an error when I add that "newline mov,ah,09h". – eLjA Sep 23 '17 at 14:36