2

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.
Keyframe
  • 1,390
  • 1
  • 14
  • 28
  • Can't see anything wrong, and the code works on linux (just changed the syscall numbers). – Jester Jul 12 '15 at 23:51
  • @Jester Now, that's odd. – Keyframe Jul 12 '15 at 23:55
  • 3
    Look at this [question](http://stackoverflow.com/questions/30814930/nasm-issue-on-osx-64-bit), it seems a similar issue that turned out to be a NASM bug. –  Jul 13 '15 at 09:51
  • @knm241oh, thank you! I have been looking for hours all over nasm, linker, code, gdb, otool. At least I honed some skills with gdb. I'll try to go back a version from 2.11.08. Post it as an answer, if you will, so I can close the question. – Keyframe Jul 13 '15 at 10:45
  • @knm241 Confirmed. It's a NASM bug. 2.11.06. works just fine. – Keyframe Jul 13 '15 at 10:50
  • 1
    Confirmed - it's a Nasm bug. Should be fixed "soon". FYI, "back ticked" strings accept "escape sequences", but are not automatically zero-terminated. `\n\0` should work. (you don't need zero-terminated strings for system calls - and don't get 'em from sys_read!) – Frank Kotler Jul 13 '15 at 13:52

0 Answers0