I've been fiddling with NASM on OSX out of curiosity and it seems I've run into a bit of an issue/problem. I'm trying to store multiple strings and their lengths in .data section and print them out.
; nasm -O0 -f macho64 -o problem.o problem.asm
; ld -macosx_version_min 10.7.0 problem.o -o problem
; ./problem
section .data
KmsgHello: db `Hello, assembly!\n`
KmsgHelloLen: equ $ - KmsgHello
KmsgEqual: db `Numbers are equal!\n`
KmsgEqualLen: equ $ - KmsgEqual
%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT 0x2000001
section .text
global start
start:
mov rdi, 1
lea rsi, [rel KmsgHello]
mov rdx, KmsgHelloLen
mov rax, SYSCALL_WRITE
syscall
mov rdi, 1
lea rsi, [rel KmsgEqual]
mov rdx, KmsgEqualLen
mov rax, SYSCALL_WRITE
syscall
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
The problem is that only the second string is printed out.
What's even more odd is that if I have a single string, for example like this:
; nasm -O0 -f macho64 -o problem.o problem.asm
; ld -macosx_version_min 10.7.0 problem.o -o problem
; ./problem
section .data
KmsgHello: db `Hello, assembly!\n`
KmsgHelloLen: equ $ - KmsgHello
KmsgEqual: db `Numbers are equal!\n`
KmsgEqualLen: equ $ - KmsgEqual
%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT 0x2000001
section .text
global start
start:
mov rdi, 1
lea rsi, [rel KmsgHello]
mov rdx, KmsgHelloLen
mov rax, SYSCALL_WRITE
syscall
mov rax, SYSCALL_EXIT
mov rdi, 0
syscall
if KmsgEqual init in .data isn't commented out, there is no output at all.
I've been using backticked string init which accepts escape sequence (in NASM) and is, I presume null terminated. I even tried with regular quotes (single) and adding 13,10,0 (newline, line feed, zero termination), but problem is still here.
So, my question is - how would one initialize multiple strings in .data and use them throughout the code?
For an advanced version of the question how would one use UTF-8 in the same situation? Note that this is just fooling around out of interest.
edit: after loading program into gdb, I get this very odd disassemble:
gdb ./problem
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Wed Feb 6 22:51:23 UTC 2013)
...
(gdb) disassemble start
Dump of assembler code for function start:
0x0000000000001fc4 <start+0>: mov $0x1,%edi
0x0000000000001fc9 <start+5>: lea 0x130(%rip),%rsi # 0x2100
0x0000000000001fd0 <start+12>: mov $0x11,%edx
0x0000000000001fd5 <start+17>: mov $0x2000004,%eax
0x0000000000001fda <start+22>: syscall
0x0000000000001fdc <start+24>: mov $0x1,%edi
0x0000000000001fe1 <start+29>: lea 0x29(%rip),%rsi # 0x2011 <KmsgEqual>
0x0000000000001fe8 <start+36>: mov $0x13,%edx
0x0000000000001fed <start+41>: mov $0x2000004,%eax
0x0000000000001ff2 <start+46>: syscall
0x0000000000001ff4 <start+48>: mov $0x2000001,%eax
0x0000000000001ff9 <start+53>: mov $0x0,%edi
0x0000000000001ffe <start+58>: syscall
End of assembler dump.