MGM2006-08-04 19:22:50
Hi everyone:
I posted this problem on some ASM forum, but did not get reply yet. I guess in part it is because not many programmer use GNU assembler and AT&T syntaxed instructions.However, the instuctions I used is very very simple. So if you are good at assembly programming, could you take a look?
('nuxrl', last time you were great help, if not too much trouble could you help? )
I am writing a compiler for a subset of Pascal. The target code is generated for GNU assembler. Now, I am testing Pascal's procedure calls of my compiler. Strange problem occurs when I execute the output assembly code. In fact, the generated code is what I expected. But I got segmentation fault each time I run it.
My testing program in pascal is very simple:
Program c2;
VAR
i,j: INTEGER;
procedure try(var a:integer; b:integer);
var x:integer;
begin
a:=b;
end;
BEGIN
i:= 5;
j:= 10;
try(j,i);
write(i);
write(j);
END.
This program should output '5 5' to the console upon compiling and execuation on Linux.

# here is the compiler-generated code
.section .data
strfmt1:
.asciz "%d"
strfmt2:
.asciz "%d\n"
.section .bss
.lcomm global_var, 8
.section .text

.globl main
main:

pushl $0
pushl $5
popl %eax
popl %ebx
addl $global_var, %ebx
movl %eax, (%ebx)
pushl $4
pushl $10
popl %eax
popl %ebx
addl $global_var, %ebx
movl %eax, (%ebx)
# by now, the assignment statements for i, j is done.
#starts to push parameters on stack
pushl $4
addl $global_var, (%esp)
#place the address of reference-parameter on stack
pushl $0
popl %eax
addl $global_var, %eax
pushl (%eax)
#place the value of value-parameter on stack
call proc_try
addl $8, %esp #clear out the parameters on stack
# start to invoke printf()
pushl $0
popl %eax
addl $global_var, %eax
pushl (%eax)
pushl $strfmt2
call printf
add $8, %esp
pushl $4
popl %eax
addl $global_var, %eax
pushl (%eax)
pushl $strfmt2
call printf
add $8, %esp
pushl $0
call exit

.type proc_try, @function
proc_try:
pushl %ebp
movl %esp, %ebp
subl $4 ,%esp
pushl $8
movl %ebp, %eax
addl %eax, (%esp) #leave the address of parameter a on stack
pushl $4
popl %eax
addl %ebp, %eax
pushl (%eax) #intend to push value of b on stack
popl %eax #%eax should contain value 5, but it does not
popl %ebx #%ebx should contain (ebp+8), but it does not
movl (%ebx), %edx
movl %eax, (%edx) #??? this line causes segmentation fault
movl %ebp, %esp
popl %ebp
ret
#------------ end of assembly code -----------
The main body of the code is fine(printf() works well). I tested using many cases. The only problem occurs when I assign value of b to reference parameter a. In the assembly code, that is the line right before the epilogue (with question marks in comment). Even I change this line to, 'movl $100, (%edx)', i got the same error, but if i remove this line: 'movl %eax, (%edx)',the program will output 5 10 on screen. So, I suspect that something went wrong when I intend to push the address of b and value of a on the stack (the value on the stack does not seem to be the desired ones)..
Can some one help me solving this issue? any suggetions?
Thanks very much..
二忽悠2006-08-04 20:20:38
有报酬吗?还是我们干完活你拿报酬?
MGM2006-08-04 20:41:07
both allowed. Ref-parameters are prefixed with VAR.
MGM2006-08-04 20:42:50
惭愧。给导师干的,没有报酬。
MGM2006-08-04 21:05:28
thanks for your suggestions. I tried objdump before...