Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a stack organized program with MARIE

I am writing a program that will evaluate the statement below, but I have to do it with a stack organized computer. This means that only pop and push can access memory.

enter image description here

How would I do this while using this program as a sample for my program?:

    Load    A           
    Store   Stack0      
    JnS Push        
    Load    B       
    Store   Stack0      
    JnS Push        
    JnS PopS        
    Load    C       
    Store   Stack0      
    JnS Push        
    Load    D       
    Store   Stack0      
    JnS Push        
    Load    E       
    Store   Stack0      
    JnS Push        
    JnS PopM        
    Load    F       
    Store   Stack0      
    JnS Push        
    JnS PopS        
    JnS PopM        
    JnS PopA        
    Load    G       
    Store   Stack0      
    JnS Push        
    Load    H       
    Store   Stack0      
    JnS Push        
    Load    K       
    Store   Stack0      
    JnS Push        
    JnS PopM        
    JnS PopA        
    JnS PopD        
    JnS Pop     
    Halt            
Push,   Hex 0       
    Load    Stack4      
    Store   Stack5      
    Load    Stack3
    Store   Stack4
    Load    Stack2
    Store   Stack3
    Load    Stack1
    Store   Stack2
    Load    Stack0
    Store   Stack1
    JumpI   Push
Pop,    Hex 0       
    Load    Stack1      
    Store   Stack0      
    Load    Stack2      
    Store   Stack1
    Load    Stack3
    Store   Stack2
    Load    Stack4
    Store   Stack3
    Load    Stack5
    Store   Stack4
    Load    Zero
    Store   Stack5
    Load    Stack0
    JumpI   Pop
PopA,   Hex 0       
    JnS Pop     
    Add Stack1      
    Store   Stack1      
    JumpI   PopA
PopS,   Hex 0       
    JnS Pop
    Load    Stack1      
    Subt    Stack0      
    Store   Stack1      
    JumpI   PopS
PopM,   Hex 0       
    JnS Pop
    Store   Calc1       
    Store   Calc3       
    JnS Pop
    Subt    One
    Store   Calc2
    Skipcond    400 
    JnS Mult        
    Store   Stack0
    JnS Push        
    JumpI   PopM
Mult,   Hex 0
LoopM,  Load    Calc3       
    Add Calc1
    Store   Calc3
    Load    Calc2
    Subt    One     
    Store   Calc2
    Skipcond    400 
    Jump    LoopM
    Load    Calc3       
    JumpI   Mult
PopD,   Hex 0       
    JnS Pop
    Store   Calc2       
    Load    Zero        
    Store   Calc3       
    JnS Pop
    Store   Calc1       
    Load    Calc2
    Skipcond    400 
    JnS Divs
    Store   Stack0
    Jns Push        
    JumpI   PopD
Divs,   Hex 0
LoopD,  Load    Calc1       
    Subt    Calc2       
    Store   Calc1       
    Load    Calc3
    Add One     
    Store   Calc3       
    Load    Calc1
    Skipcond    800 
    Jump    EndD
    Jump    LoopD
EndD,   Load    Calc3       
    JumpI   Divs
One,    Dec 1           
Zero,   Dec 0           
Calc1,  Dec 0           
Calc2,  Dec 0           
Calc3,  Dec 0
A,  Dec 9       
B,  Dec 8
C,  Dec 7
D,  Dec 6
E,  Dec 5
F,  Dec 4
G,  Dec 3
H,  Dec 2
K,  Dec 1
Stack0, Dec 0           
Stack1, Dec 0           
Stack2, Dec 0           
Stack3, Dec 0           
Stack4, Dec 0
Stack5, Dec 0

Any help would be appreciated. I am new to MARIE, and have received next to no help so far whatsoever. Thank you! =)

like image 406
Eric after dark Avatar asked Dec 06 '25 14:12

Eric after dark


1 Answers

A stack and the subroutines to leverage a stack can be implemented in MARIE with the indirection instructions.

Consider

|             LOAD ONE             
|             STOREI STACKBASE     
|             HALT                 
|                                
|  STACKBASE, HEX FFF              
|  ONE,       HEX 1       

In the MARIE Simulator, once you run the program above and scroll all the way down in the memory window to the very last row, you can see 0001 in the very rightmost position on the lowest row, memory position FFF (since MARIE has 4k 16-bit words, the last word position is 0x1000 - 1 = FFF). With this it is clear any arbitrary memory location can be written to.

Now all a programmer has to do is create and manage an OFFSET, and combine OFFSET with STACKBASE to effectively create a STACKPOINTER.

MARIE's JNS/JUMPI instructions allow us to implement subroutines, so we can make PUSH and POP subroutines that decrement/increment OFFSET and STOREI or LOADI at STACKPOINTER.

like image 68
6716 Avatar answered Dec 08 '25 17:12

6716



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!