我是x86程序員(Intel語法)的新手,並且使用內聯GCC一直在玩一些簡單的指令。我已經成功地設法處理數字和控制流程,並且現在正在使用中斷來處理標準輸入和輸出。我正在使用Mac OS X並使用-m32
GCC標誌強制編譯32位。在x86彙編中,爲什麼讀標準輸入會阻塞程序?
我有打印字符串到標準輸出如下:
char* str = "Hello, World!\n";
int strLen = strlen(str);
asm
{
mov eax, 4
push strLen
push str
push 1
push eax
int 0x80
add esp, 16
}
編譯和運行此打印Hello, World!
控制檯!但是,當我嘗試做從標準輸入一些閱讀,事情不工作,以及:
char* str = (char*)malloc(sizeof(char) * 16);
printf("Please enter your name: ");
asm
{
mov eax, 3
push 16
push str
push 0
push eax
int 0x80
add esp, 16
}
printf("Hello, %s!\n", str);
在運行時,我得到一個提示,但沒有「請輸入你的名字:」字符串。當我輸入一些輸入並按回車時,打印輸入字符串以及預期的輸出,例如,
Please enter your name: Hello, Joe Bloggs
!
如何在用戶輸入任何輸入前在預期位置顯示輸入字符串?
'fflush(stdout);'... –
沒有clobber列表的內聯程序集非常令人擔憂...... –
@KerrekSB真的,但我認爲在gcc中,可以使用'asm volatile'來防止程序集越來越搬遷。 –