C/C++中一个简单的内联汇编语言"Hello World"程序

7
我使用 DevCpp 和 Borland C 编译器...
asm {
    mov ax,4       // (I/O Func.)
    mov bx,1       // (Output func)  
    mov cx,&name   // (address of the string)
    mov dx,6       // (length of the string)
    int 0x21       // system call
}

在上面的代码片段中,我想要使用汇编语言打印一个字符串... 但是如何将字符串的地址放入寄存器cx中呢?
代码有什么错误吗?

0x21 - 厉害了我的哥,恭喜你掌握了基础知识 :-) - Preet Sangha
字符串是如何存储的?即 name 的声明是什么? - GManNickG
4
我建议忽略16位实模式汇编器,直接开始使用32位汇编器。如今,这样做更容易且更实用。 - Nils Pipenbrinck
谢谢你的回复。 但是有没有办法获取字符串的地址并将其放回cx寄存器中... 或者你尝试过内联汇编吗... 我只需要一点帮助...这样我就可以开始使用汇编语言了... 有任何示例吗? - vs4vijay
2个回答

4

我手头没有Borland编译器,所以我可能记错了它的语法,但是你尝试过这个吗:

asm {
    mov ax,4       // (I/O Func.)
    mov bx,1       // (Output func)  
    lds cx,"Hello, world" // (address of the string)
    mov dx,6       //  (length of the string)
    int 0x21       // system call
}

或者这个:
char msg[] = "Hello, world";

asm {
    mov ax,4       // (I/O Func.)
    mov bx,1       // (Output func)  
    lds cx, msg   // (address of the string)
    mov dx,6       //  (length of the string)
    int 0x21       // system call
}

编辑:虽然现在这段代码可以编译(因为我已经将MOV更改为LDS),但在运行时仍会抛出错误。我会再试一次...


1
不行啊... 它报错了........ 还有其他方法可以获取字符串的地址吗?然后再将它们放回CX寄存器中... - vs4vijay
据我所知,mov cx,msgmsg 的地址放入 cx 寄存器中。你得到了什么? - Jack
1
@vs4vijay你不应该接受无法工作的解决方案作为答案,因为这是误导性的。 - ST3

2
只需将变量名放在其中即可:
mov ax,4       // (I/O Func.)
mov bx,1       // (Output func)  
mov cx,name   // (address of the string)
mov dx,6       //  (lenght of the string)
int 0x21       // system call

免责声明:我不太擅长汇编语言。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接