News:

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

Writing bytes (mixture C and asm)

Started by Kyoy, July 13, 2005, 01:33:28 PM

Previous topic - Next topic

Kyoy

Hi, say i wish to write 5 bytes to a specific offset, can you tell me if this is wrong? I am trying to learn how to write bytes to offsets using a mixture of C and asm. This is not related to hacking of any form.


  DWORD bLocation = 0x400000;
  DWORD dwData = 0x12345678;
  BYTE bFirstByte = 0xFF;

_asm {
     mov BYTE PTR [bLocation], bFirstByte
     mov [bLocation+1], dwData
}


This means that at
[bLocation] it will be FFh
at [bLocation+1] it will be 78h
at [bLocation+2] it will be 56h
at [bLocation+3] it will be34h

Here is my question. At [bLocation+4] will it be 12h? Or will 12h be ignored since i only declared bLocation a DWORD ?

if this was done in masm, it will have resulted in an error. Can C or asm experts please help? Thx :wink

Mark Jones

Hello Kyoy. Take a look at this MASM code. It is untested but should work. It searches for the end of a string of data, making ecx a pointer to it. It then overwrites a few values there and pops up a messagebox showing the result.


include masm32rt.inc             ; std "compile-time" libs

.data
    MyString    DB  'Hello there",0,0,0,0,0   ; align-16 data /w editing room 

.code
start:
    mov ecx, offset MyString    ; put MyString offset (pointer) into ecx

@@:                             ; loop to find first null in pointer ecx
    inc ecx                     ; increment pointer one
    mov dl, byte ptr [ecx]      ; fetch byte into dl
    test dl,dl                  ; is it a null?
    jnz @B                      ; if not, keep looping

    mov byte ptr [ecx], "!"     ; put a "!" in first null
    mov byte ptr [ecx+1], "@"   ; then a "@" in second
    add ecx,3                   ; move to third null
    mov byte ptr [ecx], "$"     ; then a "$"
    inc ecx                     ; move to fourth null
    mov byte ptr [ecx], "*"     ; lastly, a "*"

    invoke MessageBox,0,addr MyString,0,MB_OK   ; display resultant string
    invoke ExitProcess,0        ; terminate gracefully
end start


Take a look at \masm32\help\ASMINTRO.HLP - very handy file. :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Mirno

bLocation + 4 will be 0x12, but you can't tell what you've overwritten to store it, chances are it'll be some part of the stack as they'll be local variables.

You don't need the asm to get the same effect though (mixing inline asm is generally bad) :

  DWORD a = 0x00400000;
  DWORD b = 0x12345678;
  BYTE   *p = (BYTE*)&a;

  p[0] = 0xFF;
  *(DWORD*)&p[1] = b;

Should have the same effect.

Mirno