NASM _printf function is not working after the memory access code

Problem Situation

I am writing a code that puts a value in memory and moves it to eax register to print. But _printf function is not working at all, there is no error and just nothing is shown on the screen.

This is the code.

section .data
   msg db "%d", 10, 0

section .text
   extern _printf
   global _main

_main:
   push ebp
   mov ebp, esp

   mov byte [402000], 100
   mov eax, dword [402000]

   push eax
   push msg
   call _printf

   leave
   ret

and I compiled with these three lines of command

nasm -f win32 memory.asm
gcc memory.obj

I executed the exe file, it is the result

NOTHING

Nothing is printed on the screen. No error, no warning, but nothing is shown on the screen

Problem Cause

I can't know the cause of this problem exactly. That's why I posted this question. But I thought about many reasons.

I'm guessing, it can be divided into three cases.

  1. Problem of _print function call process
  2. Problem with moving value to memory
  3. Problem with moving value of memory to eax

Problem of _print function call process

I checked that it is not the problem of printing part. If I just move 100 to the eax and print it out, 100 is printed well.

mov eax, 100

push eax
push msg
call _printf
100

Problem with moving value to memory

I don't think there is a problem with this.

This works without error and 100 is acceptable for byte size.

Problem with moving value of memory to eax

There might be a problem in this part.

Because I have always done like this when I move the value to eax, but it is my first time putting the value of memory into it. I wrote this code just by intuition without searching on the Internet. So, the form of this instruction may be wrong.

What I Want

[402000] <- 100
eax <- [402000]
print eax

and as a result

100

Put 100 in [402000], put the value to the eax, and print 100 by printing eax. It is the result that I want.

Thanks.



Read more here: https://stackoverflow.com/questions/66325329/nasm-printf-function-is-not-working-after-the-memory-access-code

Content Attribution

This content was originally published by dani doco at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: