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.