Beginner question about code sample
I did another tutorial and came accross the following piece of code.
Code:
; This is a simple program which displays "Hello World!"
; on the screen.
.model small
.stack
.data
Message db "Hello World!$" ; message to be display
.code
mov dx,OFFSET Message ; offset of Message is in DX
mov ax,SEG Message ; segment of Message is in AX
mov ds,ax ; DS:DX points to string
mov ah,9 ; function 9 - display string
int 21h ; call dos service
mov ax,4c00h ; return to dos DOS
int 21h
END start ;end here
My question is, why should the code be placed into the AX register before moving it to the DS. Why can't it be moved directly into the DS?
Like this:
Code:
; This is a simple program which displays "Hello World!"
; on the screen.
.model small
.stack
.data
Message db "Hello World!$" ; message to be display
.code
mov dx,OFFSET Message ; offset of Message is in DX
mov ds,SEG Message ; segment of Message is in DS
mov ah,9 ; function 9 - display string
int 21h ; call dos service
mov ax,4c00h ; return to dos DOS
int 21h
END start ;end here
Well I tried it and the program didn't work any more. But I do not understand why.
Can somebody explain this to me? I am sure this is an obvious beginner-question :).
Thanks for all help!
Re: Beginner question about code sample
SEG directive returns the segment, i.e an immediate value. According to intel instructions an imm value can only be copied to a register. That's why.
Quote:
Opcode Instruction Description
88 /r MOV r/m8,r8 Move r8 to r/m8
89 /r MOV r/m16,r16 Move r16 to r/m16
89 /r MOV r/m32,r32 Move r32 to r/m32
8A /r MOV r8,r/m8 Move r/m8 to r8
8B /r MOV r16,r/m16 Move r/m16 to r16
8B /r MOV r32,r/m32 Move r/m32 to r32
8C /r MOV r/m16,Sreg** Move segment register to r/m16
8E /r MOV Sreg,r/m16** Move r/m16 to segment register
A0 MOV AL,moffs8* Move byte at (seg:offset) to AL
A1 MOV AX,moffs16* Move word at (seg:offset) to AX
A1 MOV EAX,moffs32* Move doubleword at (seg:offset) to EAX
A2 MOV moffs8*,AL Move AL to (seg:offset)
A3 MOV moffs16*,AX Move AX to (seg:offset)
A3 MOV moffs32*,EAX Move EAX to (seg:offset)
B0+ rb MOV r8,imm8 Move imm8 to r8
B8+ rw MOV r16,imm16 Move imm16 to r16
B8+ rd MOV r32,imm32 Move imm32 to r32
C6 /0 MOV r/m8,imm8 Move imm8 to r/m8
C7 /0 MOV r/m16,imm16 Move imm16 to r/m16
C7 /0 MOV r/m32,imm32 Move imm32 to r/m32
Re: Beginner question about code sample
Thanks for your help! Now let me check if I understand your answer correctly...
So it's just a matter of certain commands only being applicable to certain registers and not to other the Data Registers. Did I get that correct?
But, just to get one thing clear...: It is not a matter of size is it? Both AX and DS are 16 bit, right?
Re: Beginner question about code sample
Answer to both questions: yes.