flat assembler
Message board for the users of flat assembler.

Index > Main > Implement Stack in FASM?

Author
Thread Post new topic Reply to topic
fasm9



Joined: 19 Jun 2003
Posts: 439
fasm9
Hi,

How to implement stack in FASM?
something display like this.

debian:~# stack
input number sequence:
1, 2, 3, 4, 5

output number sequence:
5, 4, 3, 2, 1

debian:~# _

--
Regards
Post 16 Aug 2004, 13:20
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc
Hmm, a LIFO (last in, first out) stack. Well, I have a plenty of experience with these Smile. This sample doesn't include input or output of the numbers, but it does work.

Code:
; These macros are from RetroForth
macro dup
{
        sub esi,4
        mov [esi],eax
}
macro drop { lodsd }
macro swap { xchg eax, [esi] }
macro upsh a
{
        dup
        mov eax, a
}
macro upop a
{
        mov a, eax
        drop
}



; LIFO Stack example
; Adapted from the RetroForth 4.x/5/x/6.x/7.x source code
; This implements enough of the stack to enable basic Forth primitives
; (implemented by macros) to be used. Provided primitives are dup, drop,
; and swap.
;
format ELF executable
entry start

start:
    mov esi, mystack  (ESI points to stack memory)

    upsh 1  ;  Put the numbers 1 - 5 on the stack
    upsh 2  ;
    upsh 3  ;
    upsh 4  ;
    upsh 5  ;  At this point, 1 is on the bottom and 5 is on the top

    upop ebx   ; get the top value from stack and place in ebx
                    ; subsequent calls to 'upop' will fetch the other values
                    ; from the stack

    xor eax, eax     ;   Exit the application (this is for Linux)
    inc eax             ;
    int 0x80            ;


stack_space rb 100*4   ; Reserve space for 100 numbers
mystack       rb 4          ; Start of the stack
stacK_buffer rb 10*4     ; 10 space buffer for overflows
    



Some notes on the macros:

  • upsh pushes a value to the stack
  • upop pops a value from the top of the stack
  • dup duplicates the top of the stack
  • drop drops the number on the top of the stack
  • swap switches the locations of the top two numbers on the stack


EAX holds the top item on the stack, ESI is a pointer to the stack memory. All values on the stack are 32-bit integers.

_________________
Charles Childers, Programmer
Post 16 Aug 2004, 15:49
View user's profile Send private message Visit poster's website Reply with quote
Dragontamer



Joined: 24 Aug 2003
Posts: 84
Dragontamer
If you only need temp stack space...
You could just use push/pop

But crc's is more versitile
Post 16 Aug 2004, 17:39
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-2020, Tomasz Grysztar.

Powered by rwasa.