malo malo - 1 month ago 6x
Linux Question

GNU Assembler sys_write is not working

I wrote a simple ASM Programm but sys_write is not giving any output. I guess that I did a mistake with the pointer into %ecx and sys_write can not access the string - but I do not find my mistake so far. Return code after sys_write is stored in %eax and is less 0.
There are a lot of "Hello World"-Examples but I like to understand what I am doing wrong and not the fact that there is other working code :-)

I use Intel-syntax with prefix.
the gdb output looks like this:

gdb ./testsasm
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Reading symbols from ./testsasm...done.
(gdb) break main
Breakpoint 1 at 0x4004d6: file t.asm, line 13.
(gdb) n
The program is not being run.
(gdb) run
Starting program: ...src/gnu_asm/testsasm

Breakpoint 1, main () at t.asm:13
13 mov %ebx,0x1 # file handle stdout
(gdb) n
14 mov %eax,0x4 # systemcall sys_write
(gdb) n
15 mov %ecx,string # pointer of the string const
(gdb) n
16 mov %edx,slen # string lenght
(gdb) n
17 int 0x80 # call write
(gdb) print $ecx
$1 = 1819043144
(gdb) print $edx
$2 = 7
(gdb) print *$ecx
Cannot access memory at address 0x6c6c6548

Makefile and source can be found here:


That's weird, nobody uses .intel_syntax without .intel_syntax noprefix (so you don't need the %s on register names, and $ on immediates).

You should definitely have included that in your question. (Update: oh, you did, but only buried in the text of a paragraph that looked like it was just stating what was in your gdb output). I only noticed when I looked at your full-source link since it was weird that you didn't say anything about a store to absolute address 1 segfaulting (which mov %ebx, 0x1 would in at&t syntax mode).

mov %ecx,string loads from string into ecx. With .intel_syntax noprefix, you need mov ecx, OFFSET string to get the address as an immediate constant. With intel_syntax "prefix" mode, you can probably mov %ecx, $string to get a mov r32, imm32

Put this in your ~/.gdbinit:

set disassembly-flavor intel
layout reg

Return code after sys_write is stored in %eax and is less 0

If you look it up, it should be -EFAULT for passing a bad address.