64bit - Program in x64 assembly modifying array passed from a C++ procedure in Linux does not work, though analogous solution worked for x86 -
i wrote program in x64 assembly replace string's lower-case letters stars. assembly procedure called c++ program , receives array of chars. similar logic applied x86 worked (the difference being registers used etc.), string remains unaltered when built x64. use debian linux , nasm
.
section .text global func func: push rbp mov rbp, rsp ; zadanie jako takie mov rax, qword [rbp+8] loop: cmp byte [rax], 97 jl increment cmp byte [rax], 122 jg increment mov byte [rax], 42 increment: add rax, 1 cmp byte [rax], 0 jne loop exit: mov rax, 0 ;return 0 mov rsp, rbp pop rbp ret
it called following c++ program:
#include <stdio.h> #define length 1024 extern "c" int func(char *a); int main(void) { int result; char text[length]; printf( "write string\n" ); fgets( text, length -1, stdin ); printf("string: %s\n", text); result=func(text); printf("string: %s\n", text); return 0; }
if necessary, here's makefile
:
cc=gcc asmbin=nasm : asm cc link asm : $(asmbin) -o func.o -f elf64 -l func.lst func.asm cc : $(cc) -m64 -c -g -o0 main.cc link : $(cc) -m64 -o test -lstdc++ main.o func.o clean : rm *.o rm test rm errors.txt rm func.lst
additionally, resources porting x86 x64 appreciated.
the reason program not work different calling convention in x64. see: link reference. address of string array wasn't passed on stack, case of x86, stored in rdi register. therefore solution change instruction loading array address into:
mov rax, rdi
Comments
Post a Comment