Why does using an external c function in nasm violate this code?

I ran into a problem when using the external c function to debug my nasm program.

%macro pint 1
  pushad
  push %1
  call printint
  popad
%endmacro

section .text
      extern printint
      global main
   main:
      mov eax, 3
      pint eax
      dec eax
      pint eax

      mov eax,1
      mov ebx,0
      int 0x80

while printint is defined as follows:

 void printint(int a) { 
   printf("%d\n",a);
 }

the output I get is 3 from the first print (as expected) and a random number from the 2nd print. I was told that printf () can change the values ​​of the cpu register without restoring them, so I decided to keep all the registers on the stack before calling printf, it would not allow any registers to be changed, but apparently this is not so. can someone explain why the strange conclusion and how can I fix it?

Thank.

+3
source share
1 answer

printint(), , cdecl. .

:

%macro pint 1
    pushad
    push %1
    call printint
    add esp, 4  ; Clean pushed parameter.
    popad
%endmacro
+5

Source: https://habr.com/ru/post/1782103/


All Articles