2010-02-01 77 views
7

我使用DEVCPP和Borland C编译器....在C简单的“Hello World”内联汇编语言程序/ 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字符串的地址....

是有什么错误的代码???

+0

为0x21 - 哇赞誉为获得本源:-) – 2010-02-01 18:58:38

+0

被如何存储字符串?即:“name”的声明是什么? – GManNickG 2010-02-01 19:20:19

+4

我建议忽略16位实模式汇编器,并直接从32位汇编器开始。这些日子更容易和更实际。 – 2010-02-01 19:24:03

回答

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 2010-02-02 12:55:50

+0

据我所知,'mov cx,msg'把'msg'的地址放入'cx'寄存器中。你有什么? – Jack 2013-03-23 17:51:45

+0

@ vs4vijay你不应该接受不工作的解决方案作为答案,因为它是有误导性的。 – ST3 2014-07-30 12:48:09

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 

免责声明:我不是在组装太好。