Author Topic: (SOLVED) C Program in NASM, access variable fault (write)  (Read 13093 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
(SOLVED) C Program in NASM, access variable fault (write)
« on: July 25, 2013, 01:23:20 PM »
Hello!

This is C Program in NASM.

I can access "myNumber", read value, but, when i try to change it,
then crash.

I tried many ways, but no way, so far,
there you can see some debug information.

Does anybody has any clue,
what's wrong here and
what changes needs to be made, to make it work?

Code: [Select]
[bits 32]

[SEGMENT .DATA USE32]

global _myNumber
_myNumber dd 2132

txtFormat: db "Value: %d",0

[SEGMENT .TEXT USE32]

[GLOBAL _main]
[EXTERN _printf]

_main:
push ebp
mov ebp,esp

; Print Address
mov eax,dword _myNumber

push eax
push dword txtFormat
call _printf
add esp,8

; Output -> Value: 4202496

; Print address value
mov eax,dword [_myNumber]

push eax
push dword txtFormat
call _printf
add esp,8

; Output -> Value: 2132

;Try change
mov eax, _myNumber
mov [eax],dword 1111

; Window Event Viewer:
; Faulting application problem1.exe, version 0.0.0.0,
; faulting module problem1.exe, version 0.0.0.0,
; fault address 0x0000302e.


mov esp,ebp
pop ebp
mov eax,0
ret



nasm.exe -f win32 -o program1.o program1.asm
gcc -m32 -o program1.exe program1.o



Thanks, Encryptor256!
« Last Edit: July 25, 2013, 02:25:05 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: C Program in NASM, access variable fault (write)
« Reply #1 on: July 25, 2013, 02:23:54 PM »
I found the answer!
This is VERY important => one stupid mistake and you will remember it forever! :D

It is GCC fault, be aware of how you define segment names, in lowercase or UPPERCASE.

In this case: .DATA or .data, it matters.

How i found it, how i know it?

Print map file, when generate gcc exe file,
there you can see, data segment is in lowercase,
but in my example code, segment name there is upper case.



Generate map file:

gcc -m32 -o problem1.exe problem1.o -Wl,-Map,mapfile.txt

Thanks to:
"Where the variable stored (data segment or heap or BSS) according to the variable's address?"
(http://stackoverflow.com/questions/10171073/where-the-variable-stored-data-segment-or-heap-or-bss-according-to-the-variabl)



Map file before:

You see, two segments, one is my .DATA and there is another one .data (GCC define)

Code: [Select]
.DATA           0x00402000      0x200
 .DATA          0x00402000        0xe problem1.o
                0x00402000                myNumber

.data           0x00403000      0x200
                0x00403000                __data_start__ = .
 *(.data)
 .data          0x00403000        0x0 l:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/../../../crt2.o
 .data          0x00403000        0x0 l:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/crtbegin.o
 .data          0x00403000        0x0 l:/dev-cpp/mingw32/bin/..
 ...
 ...



After:

Code: [Select]

.data           0x00402000      0x200
                0x00402000                __data_start__ = .
 *(.data)
 .data          0x00402000        0x0 l:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/../../../crt2.o
 .data          0x00402000        0x0 l:/dev-cpp/mingw32/bin/../lib/gcc/mingw32/4.7.2/crtbegin.o
 .data          0x00402000        0xe problem1.o
                0x00402000                myNumber
 *fill*         0x0040200e        0x2

END. Problem Solved!

P.S.

BTW, i think lowercase is default case always.

Edit: Thanks, Frank!
« Last Edit: July 25, 2013, 02:44:06 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: C Program in NASM, access variable fault (write)
« Reply #2 on: July 25, 2013, 02:30:33 PM »
Try changing ".data" and ".text" to lowercase. These are "known" names in "-f win32" and they are case sensitive. An uppercase name may be interpreted as an "arbitrary" name, which may be readonly. Subtle! Other than that, your code looks okay to me. (untested)

Edit: I see you found it, and my WAG was correct. Good going!

Best,
Frank