I'm working on x86-64 assembly using nasm. My task is to write a code that accepts a number as command line argument and converts it into integer. The code works normally when linked using ld linker. But if GCC linker is used, a segmentation fault occurs.
Here is the code I wrote:
Projekat.asm:
%include "Macros.asm"
section .data
section .bss
section .text
global main
main:
mov rax, [rsp + 16] ; This line is used to store the second argument (which is the number)
; into rax register
call _convertToInt
newline ; newline, exit and rax are macros located in Macros.asm
exit
_convertToInt:
mov rdi, rax
mov rax, 0
_convertLoop:
movzx rcx, byte [rdi] ; By using GDB, I found out that Segfault occurs on this line!
cmp rcx, 0
je _end
sub rcx, 48
imul rax, 10
add rax, rcx
inc rdi
jmp _convertLoop
_end:
printValue rax
ret
Macros.asm:
section .data
newline db 10, 0
newline_length equ $-newline
section .bss
digitSpace resb 100
digitSpacePos resb 8
%macro newline 0
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
%endmacro
%macro exit 0
mov rax, 60
mov rdi, 0
syscall
%endmacro
%macro printValue 1
mov rax, %1
mov rcx, digitSpace
mov [digitSpacePos], rcx
%%printValLoop:
mov rdx, 0
mov rbx, 10
div rbx
mov r14, rax
add rdx, 48
mov rcx, [digitSpacePos]
mov [rcx], dl
inc rcx
mov [digitSpacePos], rcx
mov rax, r14
cmp rax, 0
jne %%printValLoop
%%printValFinalLoop:
mov rcx, [digitSpacePos]
mov rax, 1
mov rdi, 1
mov rsi, rcx
mov rdx, 1
syscall
mov rcx, [digitSpacePos]
dec rcx
mov [digitSpacePos], rcx
cmp rcx, digitSpace
jge %%printValFinalLoop
%endmacro
I'm using 64-bit Ubuntu 20.04. Here are the commands that I used:
nasm -f elf64 -g -F dwarf Projekat.asm
ld -o Projekat Projekat.o
./Projekat 485
;In this case, the code runs normally and prints the number!
However, if I use GCC as a linker, this happens:
nasm -f elf64 -g -F dwarf Projekat.asm
gcc Projekat.o -static -o Projekat
./Projekat 485
; Causes segfault
I also used backtrace command and this is how the stack looks like:
#0 _convertLoop () at Projekat.asm:59
#1 0x0000000000401c0a in main () at Projekat.asm:13
I've been trying to solve this for 7 days now and if anyone knows what's the problem behind segfault and why is code working normally with ld and not with GCC please let me know. Thanks to all in advance!
EDIT: Changing main to _start makes the code work properly with ld linked. However, by changing _start to main and using gcc, the code no longer works.