Hi all:
I am reading some assembly documents, I realized they have different way to print to screen. What's the difference? Can someone explain the detail of difference?
Thanks.
One use:
One use:
I am reading some assembly documents, I realized they have different way to print to screen. What's the difference? Can someone explain the detail of difference?
Thanks.
One use:
.model small
.stack
.data
message db "Hello world !!!", "$"
.code
main proc
mov ax,seg message
mov ds,ax
mov ah,09
lea dx,message
int 21h
main endp
end main
One use:
.model small
.stack
.data
message byte 'Hello world !!! $'
.code
main proc
; set data segment
mov ax, @data
mov ds, ax
; print prompt
mov ah,09
mov dx,offset message
int 21h
main endp
end main
First difference:
message db "Hello world !!!", "$"
message byte 'Hello world !!! $'
The interrupt that is being called at the end requires the string to be terminated with a dollar sign. Both these codes are doing this but in different ways. Say your name is James. The first code is saying your name is "Jame" + "s" and the second code is saying your name is "James". Both are the same and will be assembled the same. There is one small thing to note is that the first code doesn't have space after the "!!!" but the second code has space. Doesn't matter really because you won't probably see the space on the screen.
--------------------------------
Second difference:
mov ax,seg message
mov ax, @data
These two are the same. @data will be translated to DS automatically. These two are the same.
--------------------------------
Third difference:
mov dx,offset message
lea dx,message
In 16 bit mode, I remember that both these two were equal. LEA performs the same as MOV XX, OFFSET YY
So these two programs are equal except for one difference, the message that is printed from the first program is:
"Hello world !!!"
while the second program prints
"Hello world !!! "
Note the space after !!!.
Hope that helps.
message db "Hello world !!!", "$"
message byte 'Hello world !!! $'
The interrupt that is being called at the end requires the string to be terminated with a dollar sign. Both these codes are doing this but in different ways. Say your name is James. The first code is saying your name is "Jame" + "s" and the second code is saying your name is "James". Both are the same and will be assembled the same. There is one small thing to note is that the first code doesn't have space after the "!!!" but the second code has space. Doesn't matter really because you won't probably see the space on the screen.
--------------------------------
Second difference:
mov ax,seg message
mov ax, @data
These two are the same. @data will be translated to DS automatically. These two are the same.
--------------------------------
Third difference:
mov dx,offset message
lea dx,message
In 16 bit mode, I remember that both these two were equal. LEA performs the same as MOV XX, OFFSET YY
So these two programs are equal except for one difference, the message that is printed from the first program is:
"Hello world !!!"
while the second program prints
"Hello world !!! "
Note the space after !!!.
Hope that helps.
Thanks for your help. My question is actually at the detail of why "mov ax,seg message" equals "mov ax, @data". Why @data can be translated into seg message? what is seg?
And does offset means "address of"? like & in C?
And does offset means "address of"? like & in C?
The MOV instruction uses the Data Segment for its source and destination if they are not specified.
For example:
Assumes that Value1 is a DWORD value defined in the data segment. Segments are "fragments" of the memory for example, in 16-bit mode, each segment is a set of 2^16 bytes or 65536 bytes. So for example, you set your Data Segment (DS) to point to byte 0..65535 in the memory, then your Code Segment to point to bytes 65536...131072 and etc. Now if you put a BYTE value in the first byte of your Data Segment, it goes to the first byte of the memory and if you put it in the first byte of the Code Segment, it will go to byte 65536, since your Code Segment starts from that address.
So basically when you want to find where a variable is, you have to first find its segment. The @data will be translated by your assembler to the data segment that is assigned to your program. So when you say:
It will find the offset of Value1 from the start of the segment that it is in or the Data Segment.
I hope that helps. It might be really vague so don't worry. You can read Intel Manuals for more description.
For example:
MOV EAX , DWORD PTR
Assumes that Value1 is a DWORD value defined in the data segment. Segments are "fragments" of the memory for example, in 16-bit mode, each segment is a set of 2^16 bytes or 65536 bytes. So for example, you set your Data Segment (DS) to point to byte 0..65535 in the memory, then your Code Segment to point to bytes 65536...131072 and etc. Now if you put a BYTE value in the first byte of your Data Segment, it goes to the first byte of the memory and if you put it in the first byte of the Code Segment, it will go to byte 65536, since your Code Segment starts from that address.
So basically when you want to find where a variable is, you have to first find its segment. The @data will be translated by your assembler to the data segment that is assigned to your program. So when you say:
MOV AX , OFFSET Value1
It will find the offset of Value1 from the start of the segment that it is in or the Data Segment.
I hope that helps. It might be really vague so don't worry. You can read Intel Manuals for more description.
And does offset means "address of"? like & in C?
To put things simply: Yes.
The long(er) answer is: It depends on particular compiler/assembler and memory model.
Listen to ti_mo_n :P