Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turbo Assembler Multiple Inputs Overlap

Tags:

assembly

tasm

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
like image 234
eLjA Avatar asked Jun 11 '26 15:06

eLjA


1 Answers

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.

like image 108
Aki Suihkonen Avatar answered Jun 14 '26 14:06

Aki Suihkonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!