Linux - 正确传递参数以供C程序使用

时间:2016-09-01 03:06:55

标签: c linux assembly

下面是我正在编写的32位函数,供Linux中的C程序使用。该代码传递两个参数(a和b)。我正在努力确保正确设置堆栈帧,以便正确打印参数(a和b)并且函数返回零。在测试期间,我的参数并不总是正确地打印从C程序传递的内容。如果你发现我遗失的任何内容,请告诉我。

bits 32

global galoisMultiply

extern  printf                       ; the C function, to be called

segment .bss                         ; for uninitialized data
section .data                        ; for initialized data
str1:   db "a value = %d", 10, 0
str2:   db "b value = %d", 10, 0

section .text                        ; for code
; Galois Field (256) Multiplication
; int galoisMultiply( int a, int  b);

galoisMultiply:    
  push ebp                   ; 2 lines are equivalent to enter
  mov ebp, esp
  sub esp, 32                ; allocate 32 bytes of local stack space
  push eax
  push ebx
  push ecx
  mov ecx, dword[ebp + 8]
  push ecx
  push  dword str1          ; string to be printed
  call printf              ; printf
  add esp, 8               ; cleanup 
  mov ecx, dword[ebp + 12]
  push ecx
  push  dword str2          ; string to be printed
  call printf              ; printf
  add esp, 8               ; cleanup 
Done:
  pop ecx
  pop ebx
  pop eax
  add esp, 32             ; deallocate 32 byte of local variable space 
  mov eax, 0              ; put the return value of p in eax
  mov esp, ebp            ; 2 lines are equivalent to leave
  pop ebp
  ret

0 个答案:

没有答案