News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Understanding immediate values

Started by unktehi, February 24, 2009, 08:52:08 PM

Previous topic - Next topic

unktehi

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

FORTRANS

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.

MichaelW

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.

eschew obfuscation

dsouza123

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.

unktehi

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!


Mark Jones

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
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

unktehi

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

RuiLoureiro

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

PBrennick

Don't forget ah, bh, ch and dh. They can accept immediate values, also.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

unktehi

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!!