看來熱心人挺多的。我的具體問題是。。

來源: MGM 2006-08-02 11:49:49 [] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (1524 bytes)
My testing program is pascal is as follows:
program a3;
var
i:integer;
begin
i:=5;
write(i);
end.

And my compiler generates the desired assembly code as follows(Intel x86 instructions in AT&T syntax, since i am generating code for the GNU assembler).The logic for assignment statement is correct,when i invoke C function,printf, I got the expected output, so the only issue is the system call 'write'.

.section .bss
.lcomm global_var, 4
.lcomm writebuffer, 4
.section .text
.globl main
main:
pushl $0
pushl $5
popl %eax
popl %ebx
addl $global_var, %ebx
movl %eax, (%ebx)
# by now, the integer value of 'i' is stored.
pushl $0
popl %eax
addl $global_var, %eax
pushl (%eax)
# the value of variable 'i' is on top of the stack
popl %edi
movl %edi, writebuffer
#move value of i to the label: writebuffer
movl $4, %eax #system call. 4-> write
movl $1, %ebx # 1->standard output
movl $writebuffer, %ecx
# move the address of writebuffer label to %ecx
movl $4, %edx
# 4 indicates the number of byte to write
int $0x80
# end of IOcall->write().

After i compile it using gcc, 'gcc -o fid fid.s', and then execute ./fid, I got strange output which indicates segmentation fault. I strictly followed the instructions from books, there is not any syntax errors or sementic errors. So weird.
THANKS VERY MUCH.

所有跟帖: 

我試過,不是exit 的緣故。有了exit call 也不行 -MGM- 給 MGM 發送悄悄話 (0 bytes) () 08/02/2006 postreply 12:51:13

strange, I attached your code to the end, still get seg-fault. -MGM- 給 MGM 發送悄悄話 (457 bytes) () 08/02/2006 postreply 13:30:53

still, not working.. -MGM- 給 MGM 發送悄悄話 (396 bytes) () 08/02/2006 postreply 14:04:13

still not working.. -MGM- 給 MGM 發送悄悄話 (396 bytes) () 08/02/2006 postreply 14:03:16

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”