The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: unktehi on February 24, 2009, 08:52:08 PM

Title: Understanding immediate values
Post by: unktehi on February 24, 2009, 08:52:08 PM
I am still confused about a lot of concepts and unfortunately my class is online and my teacher hasn't been responding to my questions.  So I'm sorry to ask basic questions - I'm just not sure where else I can go to get answers to my specific questions.

In going back, I'm reviewing the simple MOV instruction and it states that an immediate value cannot be moved to a segment. Aren't immediate values just numbers stored in a variable?

Maybe I'm also confused by the term 'segment' but I'm trying to see how the following examples from the book will throw errors:

.data
count BYTE 100
wVal WORD 2

.code
mov al, wVal
mov ax, count
mov eax, count

If these throw errors, why are the following examples valid?

move bl, count
mov ax, wVal
mov count, al
Title: Re: Understanding immediate values
Post by: FORTRANS on February 24, 2009, 10:15:01 PM
Hi,

QuoteAren't immediate values just numbers stored in a variable?

   No.  Immediate values are part of the instruction.

        MOV     AX,100

You specify the number as part of the code.

.data
count BYTE 100
wVal WORD 2

.code
mov al, wVal        ; Not valid, AL is a byte, wVal is a word
mov ax, count     ; Not valid, AX is a word, count is a byte.
mov eax, count    ; Not valid, EAX is a double word, count is a byte.

move bl, count    ; Valid, both destination and source are bytes.
mov ax, wVal          ; Valid, both destination and source are words.
mov count, al    ; Valid, both destination and source are bytes.


Steve N.
Title: Re: Understanding immediate values
Post by: MichaelW on February 24, 2009, 10:39:40 PM
You cannot move an immediate value directly into a segment register.

The first instruction below will not assemble whether in 16-bit code or 32-bit code (because there is no such instruction). The other instructions will assemble in 16-bit code or 32-bit code. In a 16-bit application that is not actually using ES to address memory they will execute without problems, but in a Windows application the first move into a segment register will trigger an access violation exception.

    ;mov es, 123  ; error A2070: invalid instruction operands

    mov ax, 123
    mov es, ax    ; OK

    mov es, mem16 ; OK

    push 123
    pop es        ; OK


QuoteImmediate values are part of the instruction

I think a better description for a beginner is that immediate operands are encoded into the instruction. For example:

mov eax, 12345678h

Assembles to:

B878563412h

Where B8h is the opcode and 78563412h is the immediate operand. The bytes are reversed (with the most significant byte at the highest address) as per the normal storage order in memory.

Title: Re: Understanding immediate values
Post by: dsouza123 on February 24, 2009, 11:03:45 PM
The parameters for mov in immediate mode are:
register, constant
variable, constant

the value of the constant is encoded as part of the instruction

a few examples

.data
  x  db   0     ; byte variable
  y  dw   0     ; word variable
  z  dd   0     ; dword variable

.code
start:
  mov al, 255
  mov ah, 0
  mov ax, 0FFFFh
  mov ax, 'AZ'
  mov eax, 4294967295
  mov eax, -1
  nop
  mov x, 254
  mov y, 0FFFEh
  mov z, 4294967294


the encoding
66B8CDAB               mov     ax,0ABCDh

segment registers are very rarely specified in typical 32 bit flat memory mode programs
(the programs usually written as normal Windows GUI or console apps)
it would be an advanced topic possibly dealing with the OS.

Segments were a common issue in 16 bit DOS mode programming.
Title: Re: Understanding immediate values
Post by: unktehi on February 25, 2009, 02:42:46 AM
FORTRANS  - Thanks for clearing up why those different instructions are invalid - that makes sense now!

Michael W - Thanks for clarifying what an immediate value is. Although you didn't put 'OK' or 'error' by it, wouldn't this instruction 'mov ax, 123'
cause an error as well?

Dsouza123 - Thanks for all the examples.

I really appreciate your help guys!

Title: Re: Understanding immediate values
Post by: Mark Jones on February 25, 2009, 03:42:15 AM
Quote from: unktehi on February 25, 2009, 02:42:46 AM
...wouldn't this instruction 'mov ax, 123' cause an error as well?

No. The 123 here is an immediate value, assumed to be decimal without any suffix, which MASM encodes into machine language similarly to the bottom of Michael's example. This "machine language" is the bit-stream that the processor can natively execute. MASM "assembler language" syntax is a 1:1 symbolic representation of machine language, for the purpose of making it easier to use. i.e., nothing is preventing a programmer from doing this:


.code
start:
blahblah BYTE 0B8h,78h,56h,34h,12h


But it is much easier to just write:


.code
start:
mov eax,12345678h
Title: Re: Understanding immediate values
Post by: unktehi on February 25, 2009, 04:35:13 PM
Ok so now I'm confused again. You're saying that in your example, the 123 is an immediate value.  But, from the presentation, it says that immediate values cannot be used in mov instructions.

I guess I'm confused as to why

this would throw an error and be incorrect:
mov es, 123  ; error A2070: invalid instruction operands

but this one would work:
mov ax, 123
Title: Re: Understanding immediate values
Post by: RuiLoureiro on February 25, 2009, 05:11:49 PM
Quote from: unktehi on February 25, 2009, 04:35:13 PM
I guess I'm confused as to why

this would throw an error and be incorrect:
mov es, 123  ; error A2070: invalid instruction operands

but this one would work:
mov ax, 123
Hi,
           es like ds, fs, gs are segment registers  (you cannot move immediate values)
           if the register is ax, bx, cx, dx, di, si or bp you can move immediate values
           the same for eax, ebx, ecx, edx, esi, edi, ebp or al, bl, cl, dl. Ok ?
Rui
Title: Re: Understanding immediate values
Post by: PBrennick on February 25, 2009, 08:35:11 PM
Don't forget ah, bh, ch and dh. They can accept immediate values, also.

Paul
Title: Re: Understanding immediate values
Post by: unktehi on February 25, 2009, 09:03:07 PM
oh wow.  Not sure why I didn't understand that before.  My brain must be overloaded with information.

That makes sense though. Thanks for your help!!