Mixing C and Assembly. `Hello World` on 64-bit Linux

前端 未结 2 1517
萌比男神i
萌比男神i 2021-01-26 13:20

Based on this tutorial, I am trying to write Hello World to the console on 64 bit Linux. Compilation raises no errors, but I get no text on console either. I don\'t

2条回答
  •  广开言路
    2021-01-26 13:57

    Okay, so my code had two mistakes:

    1) I named my as function 'write' that is common c name and i needed to rename it.

    2) in function name, i shouldn't put underscores.

    Proper code:

    writehello.s

    .data
    SYSREAD = 0
    SYSWRITE = 1
    SYSEXIT = 60
    STDOUT = 1
    STDIN = 0
    EXIT_SUCCESS = 0
    
    message: .ascii "Hello, world!\n"
    message_len =  .-message
    
    .text
    #.global main
    #main:
    #call write
    #movq $SYSEXIT, %rax
    #movq $EXIT_SUCCESS, %rdi
    #syscall
    
    #********
    .global writehello
    writehello:
    pushq %rbp
    movq %rsp, %rbp
    movq $SYSWRITE, %rax
    movq $STDOUT, %rdi
    movq $message, %rsi
    movq $message_len, %rdx
    syscall
    popq %rbp
    ret
    

    main.c

    extern void writehello(void);
    int main (int argc, char **argv)
    {
        writehello();
        return 0;
    }
    

    Compilation stays as is :) Thanks to everyone that helped!

提交回复
热议问题