Chirality
Chirality

Reputation: 745

Print Message Inline Assembly

I'm trying to print a simple hello world string to the console in inline assembly. My assembly (below) works perfectly fine. I tried translating it into GAS as best as possible but putting the variables into the registers through extended assembly proved rather difficult. From what I can tell, the printmsg function doesn't actually do/print anything.

Assembly:

section .text
   global _start

_start:
    ; Write string to stdout
    mov eax, 4
    mov ebx, 1
    mov ecx, string
    mov edx, strlen
    int 0x80

    ; Exit
    mov eax, 1
    mov ebx, 0
    int 0x80

section .data
    string  db 'Hello, World!',10
    strlen equ $ -  string

C:

#include <stdio.h>
#include <string.h>

void printmsg(char *msg, int len){
    asm(    "movl $4, %eax;"
            "movl $1, %ebx;"
       );
    asm(    "movl %1, %%ecx;"
            "movl %1, %%edx;"
            :
            : "c" (msg), "d" (len)
        );
    asm("int $0x80");
}

int main(){
    char *msg = "Hello, world!";
    int len = strlen(msg);

    printf("Len is %d\n*msg is %s\n", len, msg);

    /* Print msg */
    printmsg(msg, len);

    /* Exit */
    asm(    "movl $1,%eax;"
            "xorl %ebx,%ebx;"
            "int  $0x80"
    );
}

Upvotes: 0

Views: 5569

Answers (1)

Chirality
Chirality

Reputation: 745

Using Michael's extended assembly example:

#include <stdio.h>
#include <string.h>

void printmsg(char *string, int length){

    asm(    "int $0x80\n\t"
            :
            :"a"(4), "b"(1), "c"(string), "d"(length)
       );

}

int main(){

    char *string = "Hello, world!\n";
    int variable = strlen(string);

    /* Print msg */
    printmsg(string, variable);

    /* Exit */
    asm(    "movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );

}

Upvotes: 2

Related Questions