1

The following code works as expected, printing "Hello".

.section .data

string:
    .ascii "Hello, World!\n\0"

.section .text

.globl _start

_start:
    mov $5, %rdx            # num characters to print
    mov $string, %rcx       # address of string to print
    mov $1, %rbx            # print to stdout      
    mov $4, %rax            # print syscall
    int $0x80

    mov $1, %rax            # exit syscall
    int $0x80

I'm making a one-line change in an attempt to print the first few characters of argv[0], which should be the name of the program (ex: "/home/sam/Documents/print"). However, nothing actually seems to print. Why is this the case?

.section .data

string:
    .ascii "Hello, World!\n\0"

.section .text

.globl _start

_start:
    mov $5, %rdx            # num characters to print
    mov 8(%rsp), %rcx       # <-- attempt to print argv[0]
    mov $1, %rbx            # print to stdout      
    mov $4, %rax            # print syscall
    int $0x80

    mov $1, %rax            # exit syscall
    int $0x80

When I attempt to debug in gdb it appears that the rcx register has the correct address of the string.

x/s $rcx // 0x7fffffffe37f: "/home/sam/Documents/print"

EDITED

Code updated and works as expected.

.section .text

.globl _start

_start:
    movq $1, %rax             # print syscall
    movq $1, %rdi             # print to stdout      
    movq 8(%rsp), %rsi        # <-- attempt to print argv[0]
    movq $5, %rdx             # num characters to print
    syscall

    movq $60, %rax            # exit syscall
    syscall

  • You are trying to use 32 bit system calls on 64 bit Linux. This truncates all addresses to 32 bit, causing the problems you observe. Do not use 32 bit system calls in 64 bit programs. Let me dig up a good duplicate to link. – fuz Sep 26 '19 at 20:55
  • Thanks. Editing the original post to include now working code. – Samuel Severance Sep 27 '19 at 20:05
  • Please don't do this. If you change the question, it's hard to understand what you originally meant to ask. Instead, post an answer to your own question containing the corrected code. – fuz Sep 27 '19 at 20:09

0 Answers0