2

I'm trying to print out a single character in assembly language. The program compiles and runs without error, but it won't print anything to stdout.

Code

.section .text
 .global _start

_start:

  # push char
  push $0x41 # letter "A"

  # print char
  mov $1, %rdx # arg 3: length
  mov %rsp, %rcx # arg 2: src
  mov $1, %rbx # arg 1: file handle (stdout)
  mov $4, %rax # sys_write
  int $0x80 # call kernal

  # sys exit
  mov $1, %rax
  mov $0, %rbx
  int $0x80

If I replace %rsp with $letter and include the following:

.section .data
  letter: .byte 0x41

then the code works as expected. But I want to use the stack, since I plan to expand this program to print variable-length (null-terminated) strings from the stack, rather than a set string from .data.

I eventually want to be able to simply echo the user's input to stdout, and pushing to the stack seems like the best way, but I can't yet get this bit to work, so I want to know what it is I'm doing wrong.

Community
  • 1
  • 1
Jonathan Cowling
  • 525
  • 1
  • 5
  • 19
  • 5
    RSP is a 64-bit address (That is usually greater than can what be represented in 32-bits), this appears to be 64-bit code. The problem is that you can't use a 64-bit address with `int 0x80` . You should be using `syscall` to pass 64-bit pointers. `syscall` provides a 64-bit calling convention that differs from the 32-bit `int 0x80` so you'll need to refer to a system call reference. It happens to work for data in the `.data` section because the address likely resides in a memory address that can be represented as a 32-bit pointer. – Michael Petch Sep 14 '17 at 20:41
  • Thanks @MichaelPetch, using syscall solved the problem, I'm quite new to assembly so I'm getting tripped up by all the different architectures and syntaxes – Jonathan Cowling Sep 15 '17 at 19:33

0 Answers0