打印消息内联汇编

时间:2016-03-25 05:44:08

标签: c assembly inline-assembly

我试图在内联汇编中将简单的hello world字符串打印到控制台。我的装配(下面)工作得很好。我尝试尽可能地将其转换为GAS,但通过扩展程序集将变量放入寄存器证明相当困难。据我所知,printmsg函数实际上没有/打印任何东西。

大会:

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"
    );
}

1 个答案:

答案 0 :(得分:1)

使用Michael的扩展程序集示例:

#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"
    );

}