c - nasm, macro print not working -
i'm calling function written in assembly c file. c code passes 2 pointers assembly function. i'm using print macro assembly check value of addresses , of elements pointed them.
here c code:
extern int nn_from_set(float *q, float *t, int q_length, int *s, int s_length); int main() { float *matrix_dyn = malloc(6*4*sizeof(float)); int *s = malloc( 3 * sizeof(int)) //the vectors filled here //print address, no problem here printf("t: %p, s: %p, sizeof int*: %u\n", matrix_dyn, s, sizeof(matrix_dyn)); //assembly function called printf("prova: %d\n", nn_from_set(matrix_dyn, matrix_dyn, 3, s, 3)); return 0; }
here assembly code, it's bit long.
extern printf %macro pabc 1 ; "simple" print macro section .data .strm db %1,0 ; %1 first actual macro call section .text ; push onto stack bacwards push dword [nnfs_int] ; int push dword .strm push dword nnfs_fmt_int ; users string call printf ; call c function add esp, 12 ; pop stack 3 * 4 bytes %endmacro section .data fmt: db "%s, dist: %e",10,0 ; format string printf nnfs_fmt: db "%s",10,0 ; format string printf nnfs_fmt_int: db "%s %p",10,0 ; format string int debug section .bss nnfs_dst: resd 1 ; reserve 32-bit word nnfs_tmp: resd 1 ; int tmp; nnfs_index: resd 1 ; reserve 32-bit, int index; nnfs_int: resd 1 section .text ; function translated ; int nearest_neighbor_from_set(float* q, float* t, int q_length, int* s, int s_length) global nn_from_set nnfs_q equ 8 nnfs_t equ 12 nnfs_q_length equ 16 nnfs_s equ 20 nnfs_s_length equ 24 nn_from_set: ; ------------------------------------------- ; entrace sequence ; ------------------------------------------- push ebp ; save base pointer mov ebp, esp ; point current stack frame push ebx ; save general registers push esi push edi mov ecx, [ebp + nnfs_t] ; t mov edx, [ebp + nnfs_s] ; s mov [nnfs_int], ecx ; print t pabc "ecx, t: " mov [nnfs_int], edx ; print s ;pabc "edx, s: " mov esi, [edx] ; *s mov [nnfs_int], esi ; print *s ;pabc "edx" add edx, 4 mov esi, [edx] mov [nnfs_int], esi ;pabc "esi" ; fine di nn_from_set mov eax, 50 ; test purpose ; ------------------------------------------ ; exit sequence ; ------------------------------------------ pop edi pop esi pop ebx mov esp, ebp pop ebp ret
i have 2 problems, version of code, when try compile, says .strm defined, each time try call macro after first call.
second problem. change macro this:
%macro pabc 1 ; "simple" print macro section .text ; push onto stack bacwards push dword [nnfs_int] ; int push dword nnfs_fmt_int ; users string call printf ; call c function add esp, 8 ; pop stack 2 * 4 bytes %endmacro
i've removed .strm argument. format string now:
nnfs_fmt_int: db " reg %p",10,0
with version, first call of macro prints correctly, second goes wrong. example, call macro print addresses of 2 pointers:
mov [nnfs_int], ecx ; print t pabc "ecx, t: " mov [nnfs_int], edx ; print s pabc "edx, s: "
here output (the first line printed c file):
t: 0x8500008, s: 0x8500070, sizeof int*: 4 reg 0x8500008 reg 0xf7778898
address of t printed correctly, s's one, not. if invert 2 calls.
mov [nnfs_int], edx ; print s pabc "ecx, t: " mov [nnfs_int], ecx ; print t pabc "edx, s: "
this output (the first line printed c file):
t: 0x93a0008, s: 0x93a0070, sizeof int*: 4 reg 0x93a0070 reg (nil)
s printed right, t isn't.
i have no idea going wrong, have used same macro pattern , hasn't created me problem until now. examples tha used create macro here: http://www.csee.umbc.edu/portal/help/nasm/sample.shtml
sorry long question, , thank in advance help.
according standard calling convention, registers caller-saved, namely eax
, ecx
, edx
. such, shouldn't expect value retained through call printf
. that's presumably why wrong value printed. push second 1 on stack before printing first one, pop print too. since on stack, can reload there:
mov ecx, [ebp + nnfs_t] ; t mov [nnfs_int], ecx ; print t pabc "ecx, t: " mov edx, [ebp + nnfs_s] ; s mov [nnfs_int], edx ; print s pabc "edx, s: " ; reload since printing changed ecx , edx mov ecx, [ebp + nnfs_t] ; t mov edx, [ebp + nnfs_s] ; s
Comments
Post a Comment