1

How does the .ascii directive in x86 assembly work? When does it place the ASCII in memory? I would assume the text is part of the machine code that gets placed in main memory, but what exactly is being done in the yourText portion of the machine code here?

In 40100e, the start of the yourText code portion is placed into the buffer argument for the write syscall. In C I would interpret this as passing the pointer to the first character of a string literal, but I don't exactly get why it would go to more code rather than the spot where the literal is found in the memory.

hwa.out:     file format elf64-x86-64

Disassembly of section .text:

0000000000401000 <_start>:
  401000:       48 c7 c0 01 00 00 00    mov    $0x1,%rax
  401007:       48 c7 c7 01 00 00 00    mov    $0x1,%rdi
  40100e:       48 c7 c6 2a 10 40 00    mov    $0x40102a,%rsi
  401015:       48 c7 c2 0d 00 00 00    mov    $0xd,%rdx
  40101c:       0f 05                   syscall 
  40101e:       48 c7 c0 3c 00 00 00    mov    $0x3c,%rax
  401025:       48 31 ff                xor    %rdi,%rdi
  401028:       0f 05                   syscall 

000000000040102a <yourText>:
  40102a:       48                      rex.W
  40102b:       65 6c                   gs insb (%dx),%es:(%rdi)
  40102d:       6c                      insb   (%dx),%es:(%rdi)
  40102e:       6f                      outsl  %ds:(%rsi),(%dx)
  40102f:       2c 20                   sub    $0x20,%al
  401031:       57                      push   %rdi
  401032:       6f                      outsl  %ds:(%rsi),(%dx)
  401033:       72 6c                   jb     4010a1 <yourText+0x77>
  401035:       64                      fs
  401036:       0a                      .byte 0xa

This is the code found in this question: Hello world without using libraries, assembled then disassembled.

I've tried looking for information as to what the instructions do, but I can't find info on rex.W, and the gs at the start of insb. I can see why the program would want to put data into a port (insb), but it doesn't make sense to me to do the branching (jb) below an outsl line. I assume jb uses signals from the previous instruction.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Loogai
  • 113
  • 5

2 Answers2

7

The stuff after yourText is data, not code. The disassembler doesn't know that and tries to make sense of it as code, leading to nonsensical disassembly. You can use objdump -s to see the data in each section in a more useful representation. Match up the individual bytes and you see that it's just your string.

fuz
  • 88,405
  • 25
  • 200
  • 352
2

If the CPU did jump to that address, that's how the bytes of your .ascii would decode, as in Segmentation fault when using DB (define byte) inside a function. But in this case, those bytes aren't reached by execution because of the exit system call.

They don't need to be in the .text section; putting them in .section .rodata would let tools like objdump know not to disassemble them as code.

Other examples do that, as in Hello, world in assembly language with Linux system calls?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847