The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: theZeroFlag on October 03, 2008, 04:27:55 PM

Title: access memory directly
Post by: theZeroFlag on October 03, 2008, 04:27:55 PM
ok I have a value in ax and a value in dx

I want ax to be the first 2 bytes of EBX
and i want dx to be the last 2 bytes of EBX

how do i do this?
Title: Re: access memory directly
Post by: qWord on October 03, 2008, 04:40:38 PM
Are you asking for moving ax in lowword of ebx and dx in highword of ebx ?
If so:
mov bx,dx
shl ebx,16
mov bx,ax

-> ebx == dx:ax

regards qWord



Title: Re: access memory directly
Post by: theZeroFlag on October 03, 2008, 04:49:45 PM
ok thank you, that makes sense, we just havent studied the shl code yet.

i had though my professor had mentioned something about accessing it with bracket syntax like

      mov ax, intOperand1
      imul intOperand2
      mov WORD PTR [ebx], ax
      mov WORD PTR [ebx+2], dx
Title: Re: access memory directly
Post by: Mark Jones on October 03, 2008, 04:58:34 PM
Also when dealing with memory addresses, keep in mind that byte order written to (and read from) memory is reversed -- so if EAX was 0x00000012h, when written to memory it would be stored as 0x12000000h (and when read back into eax, would be 0x00000012h again.) So in this example, if you then wrote EBX to a memory offset, don't be surprised to see it stored reversed. When that value is read back into EDX from memory though, it would once again be in the correct order.

What may be really helpful is writing some simple code such as this, then stepping through its execution in a debugger such as OllyDbg (http://ollydbg.de/). Then the effects of each instruction can be seen easily.
Title: Re: access memory directly
Post by: qWord on October 03, 2008, 05:02:22 PM
Quotei had though my professor had mentioned something about accessing it with bracket syntax like

     mov ax, intOperand1
     imul intOperand2
     mov WORD PTR [ebx], ax
     mov WORD PTR [ebx+2], dx

the result is written to an 32bit memory-location pointed by ebx


PS: why starting two discussions with the same topic ?
Title: Re: access memory directly
Post by: theZeroFlag on October 03, 2008, 05:13:49 PM
oh ok, i see what i was doing

i should have moved the offset of some piece of memory into EBX and then accessed from there

a little confusing but this is what i came up with

      mov ax, intOperand1
      imul intOperand2
      mov EBX, offset doubleResult
      mov WORD PTR [ebx], ax
      mov WORD PTR [ebx+2], dx