2010-02-01 124 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 

免責聲明:我不是在組裝太好。