MARIE Assembly Sorting Numbers

Thread Starter

tylercross

Joined Apr 8, 2019
1
Hello , I am trying to create a code that sorts the a list of three integers, I have some code but I do not know why it is not working correctly.
Code:
    Input  
    Output  
    Store    A
    Input  
    Output  
    Store    B
    Input  
    Output  
    Store    C
    Load    A
    Subt    B
    Skipcond    800
    Jump    Next
    Load    A
    Store    Temp
    Load     B
    Store    A
    Load     Temp
Next,    Load    B
    Subt    C
    Skipcond    800
    Jump    Next2
    Load    B
    Load    Temp
    Store    C
Next1,    Load    A
    Subt     B
    Skipcond    800
    Jump    Next2
    Load    A
    Store    Temp
    Load     B
    Store    A
    Load     Temp
Next2,    Load    A
    Output  
    Load    B
    Output  
    Load    C
    Output  
    Halt  
A,    Dec    0
B,    Dec    0
C,    Dec    0
Temp,    Dec    0
Mod edit : code tags
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Welcome to AAC!

I don't know much about MARIE but here's the assembler language reference.
https://github.com/MARIE-js/MARIE.js/wiki/MARIE-Instruction-Set-(with-Opcodes)

You have no comments in the code. Comments are an important debugging tool. I started to comment your code from the assembler language reference so I could learn some MARIE and that process identified an error in your first swap.
Review that and comment the rest of the code. You may find other errors that way. Get a pencil and paper, draw the code and data flow then trace it step by step to see if it makes sense.

At the top of the file, add a few lines to tell other readers what the code is supposed to do.
Code:
  Input  // Get user values to A,B, and C.  NOTE: AC is the accumulator
  Output 
  Store  A
  Input 
  Output 
  Store  B
  Input 
  Output 
  Store  C
  // Begin the sort
  Load  A  // AC<-A
  Subt  B  // AC= A-B
  Skipcond  800  // Skip if A>B (AC>0)
  Jump  Next  // Jump if A<=B
  Load  A  // A>B so swap A and B
  Store  Temp  // A->Temp
  Load  B  // AC<-B
  Store  A  // AC->A
  Load  Temp  // AC<-Temp
  // ***ERROR: you dont store Temp to B so the swap is incomplete
Next,  Load  B
  Subt  C
  Skipcond  800
  Jump  Next2
  Load  B
  Load  Temp
  Store  C
Next1,  Load  A
  Subt  B
  Skipcond  800
  Jump  Next2
  Load  A
  Store  Temp
  Load  B
  Store  A
  Load  Temp
Next2,  Load  A
  Output 
  Load  B
  Output 
  Load  C
  Output 
  Halt 
A,  Dec  0
B,  Dec  0
C,  Dec  0
Temp,  Dec  0
If you look at the bottom of this thread you'll see suggestions of other threads to visit. A couple of those have suggestions for writing and debugging MARIE.
Good luck!
 

MrSoftware

Joined Oct 29, 2013
2,273
Write pseudo code for what your program is SUPPOSED to do. Work it out by hand and make sure the pseudo code is correct. Then start comparing your pseudo code to your real code and I bet the problem will become apparent.
 
Top