GNU assembler syntax for strlen

My Hello World assembler only works if I hardcode the message length. If I try to use pseudo-ops to calculate the length, the program either prints extra garbage, or fails to build.

Code:

msg: .asciz “Hello World!\n”

push $14
push $msg

Trace:

$ clang -c -o hello.o -arch i386 hello.s
$ ld -o hello -macosx_version_min 10.6 -arch i386 hello.o
$ ./hello
Hello World!

Code:

msg: .asciz “Hello World!\n”
len: .long .-msg

push $len
push $msg

Trace:

$ clang -c -o hello.o -arch i386 hello.s
$ ld -o hello -macosx_version_min 10.6 -arch i386 hello.o
$ ./hello
Hello World!
?';? msgstdoutsys_exitsys_writekernel__mh_execute_headerstart

Code:

msg: .asciz “Hello World!\n”
len = .-msg

push $len
push $msg

Trace:

$ clang -c -o hello.o -arch i386 hello.s
$ ld -o hello -macosx_version_min 10.6 -arch i386 hello.o
ld: section __data (address=0x00002000, size=4294967279) would make the output executable exceed available address range for architecture i386

What is the proper syntax for calculating the length of a hardcoded string in Clang/Gas? In NASM, I write len equ $-msg

msg: .asciz "Hello, world!\n"
.set len, . - msg

foo:
  push $len

msg: .asciz “Hello, world!\n”
.set len, . - msg

foo:
push $len

That’s odd, on my system this syntax fails to link.

$ clang -c -o hello.o -arch i386 hello.s
$ ld -o hello -macosx_version_min 10.6 -arch i386 hello.o
$ ld: section __data (address=0x00002000, size=4294967279) would make the output executable exceed available address range for architecture i386

Joerg, could you post a full hello.s file?

What are your system specs? Mine are:

$ ld -v
@(#)PROGRAM:ld PROJECT:ld64-134.9
configured to support archs: armv6 armv7 armv7s i386 x86_64
LTO support using: LLVM version 3.1svn, from Apple Clang 4.1 (build 421.11.65)
$ clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
$ xcodebuild -version
Xcode 4.5.1
Build version 4G1004
$ system_profiler SPSoftwareDataType | grep System
System Software Overview:
System Version: OS X 10.8.2 (12C54)

It's just the relevant fragment. Tested on ELF, not sure about OSX.

Joerg

The following links, runs, and returns an exit code of 15 for me on OSX:

.data
msg: .asciz "Hello, world!\n"
.set len, . - msg

.text
.globl _main
_main:
        push $len
        pop %eax
        ret

-Eli

The following links, runs, and returns an exit code of 15 for me on OSX:

.data

msg: .asciz “Hello, world!\n”
.set len, . - msg

.text
.globl _main
_main:
push $len
pop %eax
ret

Good to know. I copied this snippet to test.s and tried building on my Mac.

$ clang -c -o test.o -arch i386 test.s
$ ld -o test -macosx_version_min 10.6 -arch i386 test.o
Undefined symbols for architecture i386:
“start”, referenced from:
-u command line option
ld: symbol(s) not found for architecture i386

So I rename _main to start, reassemble, and continue linking.

$ clang -c -o test.o -arch i386 test.s
$ ld -o test -macosx_version_min 10.6 -arch i386 test.o
ld: section __data (address=0x00002000, size=4294967307) would make the output executable exceed available address range for architecture i386

Eli, what are your build commands? Which versions of the developer tools are you using?

I finally managed to write a hello.s that builds and runs without problem, using as. clang refuses to assemble it.

I was just compiling with "clang test.s"; using trunk clang on OS X 10.8.

-Eli