flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Handling subroutine parameters

Author
Thread Post new topic Reply to topic
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 20 Mar 2006, 14:35
I have run into a problem with my compiler for a basic-like language.

When a sub is called, its parameters are pushed onto the stack before I use a call instruction to transfer control to the procedure.

At the moment I have this code:
Code:
; Function with two local variables:
; Apple.l  = [ebp+0]
; Orange.l = [ebp+4]
jmp cl_end_myfunction
cl_myfunction:
push ebp      ; store the stack frame
push ecx      ; preserve registers
push edx
mov  ebp, esp ; create new stack frame
sub  esp, 8   ; allocate two longs (Apple and Orange)

mov  dword [ebp+4], 2    ; move 2 into Orange
mov  dword eax, [ebp+4]  ; move Orange into eax
mov  dword [ebp+0], eax  ; move eax into Apple
xor  eax, eax
mov  dword eax, [ebp+0]  ; move Apple into eax
mov  [v_a], eax
cl_end_myfunction

xor  eax, eax ; return zero
add  esp, 8   ; deallocate Apple and Orange
pop  edx
pop  ecx
pop  ebp ; restore the stack frame
ret  0   ; return and deallocate the parameters (0 here)

call cl_myfunction    


The problem is, how do I know where the parameters are, since they are pushed before the registers? How should I reference them?

_________________
Roses are red
Violets are blue
Some poems rhyme
And some don't.
Post 20 Mar 2006, 14:35
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 20 Mar 2006, 17:12
I would be interested in more info on your " basic-like language" as we are looking for something like this for a asm OS me and a group of programmers are making.
We have a fasm and retroforth port, but are looking at ways to add high language support, for beginners etc.
Thanks.
Post 20 Mar 2006, 17:12
View user's profile Send private message Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu 20 Mar 2006, 17:31
You are NOT creating the stack frame corectly.

Study how HLL languages like C and PAscal do create it.

This is TASM syntax but FASM has some HLL like macros that create similar or relatively close syntax:

Code:
My_PROC PROC STDCALL
  USES esi,edi
  ARG @@a_src:dword, @@a_dest:dword
  LOCAL @@tmp_data:dword

  mov esi,[@@a_src]
  mov edi,[@@_dest]

  mov eax,[esi+ SRC_STRUC.member_01]
  mov [@@tmp_data],eax

  ret
ENDP
    


Arguments are stored at EBP + some offset (figure out the value as an exercise)
LOCAL variables are stored at EBP - some offset
ESP is placed below LOCAL variables So any pushes/pops will not affect them.
You must not change EBP once the stack frame is created.

The trick is to use EBP as a fixed reference point on the stack, that is why it is called "a frame": something above and somethiong below a fixed point....

You have an error into your stack frame above and this generates your confusion Wink
Post 20 Mar 2006, 17:31
View user's profile Send private message Visit poster's website Reply with quote
bogdanontanu



Joined: 07 Jan 2004
Posts: 403
Location: Sol. Earth. Europe. Romania. Bucuresti
bogdanontanu 20 Mar 2006, 17:48
Just in case, the corect frame creation goes like this:

Code:
; prologue
push ebp
mov ebp,esp
add esp, size_of_local variables

; your code here


; epilogue
mov esp,ebp
pop ebp
ret size_of_arguments
    


Or the shorter version using enter and leave
Post 20 Mar 2006, 17:48
View user's profile Send private message Visit poster's website Reply with quote
Kain



Joined: 26 Oct 2003
Posts: 108
Kain 21 Mar 2006, 18:30
>add esp, size_of_local variables

Slight slip here, Bogdan means:

sub esp, size_of_local variables


IMO, it's almost always better to let a well tested macro create the stack frame. I've run into many problems with easy to miss minor errors of this sort when creating my own frames, plus you get the added bonus of not having to manually calculate the offsets.
Post 21 Mar 2006, 18:30
View user's profile Send private message Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue 22 Mar 2006, 13:58
Kain wrote:
>add esp, size_of_local variables

Slight slip here, Bogdan means:

sub esp, size_of_local variables
That's what I'm already doing, I think. bogdanontanu needs to tell me what exactly I've done wrong with the creation of the stack frame. I'm not capable of figuring it out myself.

Dex4u, the compiler is not written in asm, it only outputs asm.

_________________
Roses are red
Violets are blue
Some poems rhyme
And some don't.
Post 22 Mar 2006, 13:58
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 22 Mar 2006, 18:39
Plue wrote:
Dex4u, the compiler is not written in asm, it only outputs asm.
Never mind, thanks anyway.
Post 22 Mar 2006, 18:39
View user's profile Send private message Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509 22 Mar 2006, 22:47
Dex4u wrote:
Plue wrote:
Dex4u, the compiler is not written in asm, it only outputs asm.
Never mind, thanks anyway.


The simply solution would be to rewrite the compiler in it's own language, and since the compiler outputs asm, then you get your asm source for the compiler!

(Just like what I'm working on, with the current b0 compiler. I'm about 70% of the way through now).
Post 22 Mar 2006, 22:47
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 22 Mar 2006, 23:57
Yes that would be good, if not i need a basic or pascal compiler written in fasm.
Post 22 Mar 2006, 23:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.