Reputation: 3
I am testing use of .bss for allocation of a memory area to hold a single number. Then print that number to console. The output is not as expected. I am supposed to get e number (12), but get a newline.
System config:
$ uname -a
Linux 5.8.0-48-generic #54~20.04.1-Ubuntu SMP Sat Mar 20 13:40:25 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
description: CPU
product: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
The code:
# compile with: gcc -ggdb -nostdlib -no-pie test.s -o test
.bss
.lcomm output,1
.global _start
.text
_start:
# test .bss and move numer 12 to rbx where memory are allocated in .bss
mov $output, %rbx # rbx to hold address of allocated space
mov $12,%rdx # Move a number to rdx
mov %rdx,(%rbx) # Move content in rdx to the address where rbx points to (e.g ->output)
# setup for write syscall:
mov $1,%rax # system call for write, according to syscall table (http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/)
mov $1,%rdi # fd = 1, stdout
mov $output,%rsi # adress of string to output moved to rsi
mov $1,%rdx # number of bytes to be written
syscall # should write 12 in console
mov $60,%rax
xor %rdi,%rdi
syscall # exit normally
I have set a breakpoint with the first syscall (using GDB), to look into the registers:
i r rax rbx rdx rdi rsi
rax 0x1 1
rbx 0x402000 4202496
rdx 0x1 1
rdi 0x1 1
rsi 0x402000 4202496
x/1 0x402000
0x402000 <output>: 12
The output after syscall is blank, was expected to get the number "12":
:~/Dokumenter/ASM/dec$ gcc -ggdb -nostdlib -no-pie test.s -o test
:~/Dokumenter/ASM/dec$ ./test
:~/Dokumenter/ASM/dec$ ./test
:~/Dokumenter/ASM/dec$
So, my question is, are there any obvious explanation of why I am getting blank and not 12 ?
Upvotes: 0
Views: 214
Reputation: 881093
mov $output,%rsi # address of string to output moved to rsi
^^^^^^
Address of string. The value $12
is not the character sequence "12"
. If you wanted to print the string 12
, you would need to load 0x31
and 0x32
('1'
and '2'
) into the memory area (making it big enough) the use 2
as the length.
For example, movw $0x3231, output
or better movw $0x3231, output(%rip)
to use RIP-relative addressing for static data, like normal for x86-64. (Unlike NASM, GAS syntax doesn't $'12'
as a way to write the same integer constant.)
If you want to print an integer as a string, you'll probably want to manipulate it mathematically so you can do it one digit at a time. (Printing an integer as a string with AT&T syntax, with Linux system calls instead of printf)
Upvotes: 2