0
This is the code (rid of most comments '#' for readability. "..._nude.s" in example namefile refers to this): 
  1 #PURPOSE: This program is to demonstrate how to call printf
  2 
  3  .section .data
  4 
  5 firststring:
  6  .ascii "Hello! % is a %s who loves the number %d\n\0"
  7 name:
  8  .ascii "Jonathan\0"
  9 personstring:
 10  .ascii "person\0"
 11 numberloved:
 12  .long 3
 13 
 14  .section .text
 15  .globl _start
 16 _start:
 17  pushl  numberloved     #This is the %d
 18  pushl  $personstring   #This is the second %s
 19  pushl  $name           #This is the first %s
 20  pushl  $firststring    #This is the format string in the prototype
 21  call   printf
 22 
 23  pushl  $0
 24  call   exit

$ as printf-example_nude.s -o printf-example_nude.o

$ ld printf-example_nude.o -o printf-example_nude -lc -dynamic-linker /lib/ld-linux.so.2

$ ./printf-example_nude
zsh: segmentation fault (core dumped) ./printf-example_nude

(As we can see and i followed the assembly/linking instructions from the book:

  1. Assembly went well.
  2. Linking went well.
  3. Execution failed. Machine: Linux (Debian) 32-bit.)

Any idea? Thank you.

nostromo
  • 61
  • 1
  • 1
  • 11
  • Spelling mistake. Line 6: should be %s. Now run perfect: $ ./printf-example_nude Hello! Jonathan is a person who loves the number 3 – nostromo Dec 21 '22 at 18:15
  • Note that an important piece of the calling sequence is missing: cleaning up the stack after the call to printf (in the SysV ABI this is the caller's responsibility). There should be an `add $16, %esp` after `call printf`. There's also no attention to stack alignment. – Nate Eldredge Dec 21 '22 at 18:16
  • Thank you Mr. Eldredge. I will also include in my novice reflections, your point about the ABI convention and i will try to discover in future readings about the "always present" topic about stack alignment. By the way, is there any text (paper o part of book. [i am on several books but perhaps i didn't reach already the alignment chapter]) where this thing about the omnipresent "stack alignment" appears really straight and clear? Thank you for your time. – nostromo Dec 21 '22 at 18:26
  • See https://stackoverflow.com/questions/40307193/responsibility-of-stack-alignment-in-32-bit-x86-assembly for an explanation of stack alignment for 32-bit SysV ABI, and https://stackoverflow.com/questions/64729055/what-does-aligning-the-stack-mean-in-assembly, https://stackoverflow.com/questions/49391001/why-does-the-x86-64-amd64-system-v-abi-mandate-a-16-byte-stack-alignment for 64-bit. – Nate Eldredge Dec 21 '22 at 18:37
  • In short: whenever you call a C-compiled or library function, you must ensure that immediately before executing the `call` instruction, the stack pointer equals a multiple of 16. If you are being *called* by a C function, you can likewise assume that on entry, the stack pointer is 4 less than a multiple of 16 (or 8 less in 64-bit code), so you must subtract a further 12 (respectively 8) to get back to a multiple of 16. However this does not apply at a `_start` entry point since it is jumped to by the kernel, not by the C library or startup code, so alignment is not guaranteed. – Nate Eldredge Dec 21 '22 at 18:40
  • It isn't something that a book should postpone to a later chapter, since on modern systems, if you call a library function without proper stack alignment, in many cases it will just crash. I'm not sure if your book is obsolete or carelessly written, but either way it may not be a good source for you. – Nate Eldredge Dec 21 '22 at 18:42
  • I am reading: Hyde's, Duntemann's, Bartlett's, Carter's, Peter's, Seyfarth's... Most 32 bit architecture and some 64 bit. I use VirtualBox to run 32 bit (on my native 64 bit). Trying to do all the examples. Starting "inertial" no-brainer... Hoping in 10 years i get some true ground and "fluency". Was a nightmare creating all the enviroments from scratch (hoping coping with nightmares could give some knowledge-nectar). – nostromo Dec 21 '22 at 20:09
  • Any better books/ideas to steep & purge the learning curve? (The philosophy of reading many different books/authors is like intersecting sets. So i can grasp more the essence and avoid personal bias (although some personal comments are very good). Any advice on this? What books would you recommend for rich (no shortcuts) but "to the point" learning over this? Thank you! – nostromo Dec 21 '22 at 20:09
  • Works for me with `gcc -m32 -no-pie -nostartfiles print.s` on x86-64 Arch Linux. Also works with `as --32 print.s -o printf-example_nude.o` ; `ld -m elf_i386 printf-example_nude.o -o printf-example_nude -L/usr/lib32 -lc -dynamic-linker /lib/ld-linux.so.2`. IDK what's wrong with your system. It actually does keep `%esp` aligned by 16, @NateEldredge, since it does four 4-byte pushes after entering user-space with ESP aligned by 16. (except on the call to `exit`; `movl $0, (%esp)` ; `call exit` would reuse the outgoing arg space you allocated with push). – Peter Cordes Dec 22 '22 at 03:05
  • Perhaps `ld` found a `libc.a` static library on your system instead of a `libc.so`? Unlikely since you asked it make a dynamic executable and didn't use `-static`. But if so, it could explain libc init functions not having been called from dynamic linker hooks, resulting in a crash when you do non-standard stuff like calling libc functions from `_start` without having manually called glibc's init functions. This would definitely crash if statically linked - [Static linkage with glibc without calling main](https://stackoverflow.com/q/26358119) – Peter Cordes Dec 22 '22 at 03:09
  • What instruction crashes? Run your program under `gdb` to find out. Presumably its inside libc somewhere, from `call printf`. Also related: [Linking a program using printf with ld?](https://stackoverflow.com/q/55314762) and [Assembler output does not run on my Linux machine](https://stackoverflow.com/q/58748846) – Peter Cordes Dec 22 '22 at 03:11
  • @nostromo: There is a curated list of x86 assembly language resources at https://stackoverflow.com/tags/x86/info – Nate Eldredge Dec 23 '22 at 16:25
  • Uau! Wonderful! Looks ultra-complete. Thank you. Happy Merry Christmas and New Year Entering! ;D – nostromo Dec 23 '22 at 16:52

0 Answers0