Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrangement of Variables

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hello. I am very new to assembly and I was messing around with a short little program to get familiar with the basics but something I thought was odd kept happening. The program is suppose to get a character from the keyboard and then display that character in this format: (character). Or if you hit escape it will say: Esc. I have 4 variables. Two contain the ( and ) another contains a string telling the user to input a character and the last one to store the input entered. I discovered, however, that if I don't declare the variables that contain the ( and ) first, instead of outputting the character like (a) it will output aa) or (aa. Can anyone please shed some light on this? I'd greatly appreciate it. Heres the code that messes up:

JMP start
;========================================
instr db "Enter A Character:",10,13,"$"
key db
leftbr db "("
rightbr db ")"

;========================================
start:
mov ah,09
mov dx,offset instr
int 21h
mov ah,08
int 21h
mov key,al
CMP key,27
JNZ print
JZ escape
print:
mov ah,02
mov dl,leftbr
int 21h
mov dl,key
int 21h
mov dl,rightbr
int 21h
JMP exit
escape:
mov ah,02
mov dl, "E"
int 21h
mov dl, "s"
int 21h
mov dl, "c"
int 21h
exit:
mov ah,4Ch
mov al,00
int 21h
 
you have this:
key db
leftbr db "("

There's no value after the db of key. So the Assembler looks at key db, it sees nothing to put after, so it puts nothing after it. It doesn't allocate ANY space for key, it does nothing. So key and leftbr actually occupy the same space!!! So when you mov key,al, you accidentally do mov leftbr,al. So leftbr now contains the key.

To solve the problem, do this:
key db ?
leftbr db "("

the ? means "leave one space" where that one space is long enough for the type. It's a db, define byte, so it leaves one byte.

The assembler should have flagged this with a warning, though. Seems you got a lousy assembler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top