Jump to content

mov [input1],eax

- - - - -

  • Please log in to reply
7 replies to this topic

#1
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
My PDF on NASM says to use the syntax:


mov [input1],eax


to store the value in eax into the memory location given by input1. Why is this so? Doesn't putting square brackets around an identifier mean to access the contents of input1? This is supposed to use the address of input1 as the destination operand. To use the value stored at that address just seems silly.

This is the code that NASM accepted. It produces an error if I don't dereference input1.

input1 is a label for an uninitialized storage location defined in .bss, if that's at all relevant.
Programming is a journey, not a destination.

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
How is that silly? input1 is simply a pointer. Actually, it is like a C macro, it just gets translated directly into a memory address by the assembler. The brackets tell it to access the data stored at the memory address pointed to by input1.
Latinamne loqueris?

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
input1 is an address. You can't move a value into an address.

Let's say input1 is EBX. The following code:
%define input1 ebx 

mov input1, eax 
would be translated to
mov ebx, eax 
So basically you are saying 'EBX=EAX' , what I'm sure you don't want to do.

What you need to do, is you need to access the memory at that address, which is what the brackets are for:
%define input1 ebx 

mov [input1], eax 
and
mov [ebx], eax 

What's so silly about telling the processor to access memory (and not the pointer)?

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Basically what I wanted to say, I've never been good at explaining stuff :D
Latinamne loqueris?

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
My understanding was that, if input1 is an address, and it stores the value 24, then [input1] will evaluate to 24, since it dereferences it. So

mov [input1],eax

would copy eax into 24.
Programming is a journey, not a destination.

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
Well, no. [input1] will only become 24 if you're using [input1] as a source operand. In the 'mov [input1], eax' case, you are using [input1] as a destination operand, what changes the way it's interpreted.

'mov eax, [input1]' would in your example set EAX to 24.
'mov [input1], eax', on the other hand, would not evaluate [input1] to 24, but rather would replace the 24, in memory, with whatever is currently in EAX.

In other words, if you use [input1] as source, you're reading from memory; but when you use [input1] as destination, the processor - instead of reading the value from that address in memory - writes the value from the source operand to the destination operand.

mov eax, [input1] 
would do something like this:
read { type:memory; address:input1; } 

store { place:EAX } 

while this code:
mov [input1], eax 
does something like this:
load { place:EAX } 

write { type: memory; address:input1; } 

So yeah, writing to memory is a little different from reading from memory.

#7
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Okay, makes sense now.
Programming is a journey, not a destination.

#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I take it you were thinking of 'variables'. There is no concept of variables in assembly.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users