ASM strings get code corrupted
Question
I have a simple 16 bit assembly like the following:
push bx
StrVar db "My string!",0
push ax
.. other code..
I'm trying to compile it with NASM, but I have a question.. if my string with the trailing zero is not a multiple of the 16 bits (let's say, 19 bytes zero included) the asm following the string is bad-aligned and gets messed up (I saw it into the disassembler).
Why is that so? I noticed opcodes aren't always 16 bit, so how the disassembler decide what boundaries should the opcodes follow?
Besides.. how can one prevent this from happening? Should I count my strings every time each character?
Solution
Your problem is that as it stands right now, your code doesn't make much sense. After executing push bx
, it's going to fetch the first few bytes of your string, and attempt to execute them as instructions. That generally won't work very well.
Normally, you want to define data in a separate segment from the code. In MASM syntax, you might use something like:
.data
StrVar db "My String!", 0
.code
; ...
push bx
push ax
mov si, offset StrVar
; ...
There are times you pretty much need to define data in the code segment, but when you do you normally need to use flow control instructions to ensure it never gets executed (e.g., a jump table inserted immediately after a ret
from a function).
OTHER TIPS
It's probably your disassembler getting confused because you're mixing instructions and data like this.