I am currently learning assembly language. I have been able to create a quick short function to swap two numbers from least to greatest. I am applying the same basic foundation to do this with three numbers but every time I am performing a comparison it goes into an infinite loop. I am declaring this function by using *60
. How can I properly sort three numbers from least to greatest? Also, is there a way to have the same function perform sorts of two and three numbers without any additional changes?
Several assembly programs have slight change in syntax. HERE is the link of an educational assembly Little Man computer simulator that I am currently using.
Working Swap with two numbers:
INP //Input x number
STO 99 //Store x number
INP //Input y number
STO 98 //Store y number
BR 60 //Jump to function *60
HLT //End run
*60 //Number SWAP function
LDA 99 //Load x
STO 87 //Move x to mailbox 87
LDA 98 //Load y
STO 86 //Move y to mailbox 86
SUB 87 //Subtract y - x
BRP 71 //Branch to line 71 if result is positive
LDA 86 //SUB 87 gives a negative result- then y is smallest number. Load y
OUT //Display y
LDA 87 //Load x- the greater number.
OUT //Display x
HLT //End here since y > x
LDA 87 //BRP 71 branches here- then x is smallest number
OUT //Display x
LDA 86 //y is the greater number
OUT * //display y