Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > BOOK: Professional Assembly Language
|
BOOK: Professional Assembly Language
This is the forum to discuss the Wrox book Professional Assembly Language by Richard Blum; ISBN: 9780764579011
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Assembly Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2006, 12:34 PM
mgm mgm is offline
Registered User
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Having problem inside functions (seg-fault).

Hi everyone:
  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.
# 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
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. 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. 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..

Tony








Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem accessing global variable inside functions jedijr88 Beginning PHP 3 June 29th, 2007 12:51 PM
Segmentation Fault diamondField Linux 4 December 15th, 2006 12:12 PM
Problem with datagrid inside a datalist hoeknu ASP.NET 1.0 and 1.1 Professional 3 April 4th, 2006 10:45 PM
Wrox example code fault ) The Gorgon BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 August 9th, 2004 01:12 AM
Problem with JScript inside XSLT chrisybhoy XSLT 6 September 17th, 2003 07:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.