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..
向匯編高手們求助,function calls in ASM 。
所有跟帖:
• 有報酬嗎?還是我們幹完活你拿報酬? -二忽悠- ♂ (50 bytes) () 08/04/2006 postreply 12:20:38
• 慚愧。給導師幹的,沒有報酬。 -MGM- ♂ (0 bytes) () 08/04/2006 postreply 12:42:50
• both allowed. Ref-parameters are prefixed with VAR. -MGM- ♂ (114 bytes) () 08/04/2006 postreply 12:41:07
• thanks for your suggestions. I tried objdump before... -MGM- ♂ (418 bytes) () 08/04/2006 postreply 13:05:28