lego69
lego69

Reputation: 857

swap on assembly

I wrote swap on assembly, but I'm not sure that my code is right, this is the code

swap:  mov r1, -(sp)   

   mov (sp) r1
   mov 2(sp) (sp)
   mov r1 2(sp)

   mov (sp)+, r1
   rts pc

swap receives pointer from stack

Upvotes: 2

Views: 570

Answers (2)

Mikhail Gusev
Mikhail Gusev

Reputation: 1

This code:

    mov #1,-(sp)
    mov #2,-(sp)
    jsr pc,swap
    halt

swap:              
    mov 2(sp),-(sp)  ; a,b,ret -> a,b,ret,b
    mov 6(sp),4(sp)  ; a,b,ret,b -> a,a,ret,b
    mov (sp)+,4(sp)  ; a,a,ret,b -> b,a,ret
    rts pc

Better using another register for data stack

Upvotes: 0

Joelmob
Joelmob

Reputation: 1106

is sp a stackpointer? There is usually the command ldw rA, 0(rB) (0 is the offset and rB is the address you will load from, the actual data is now in rA). ldw loads a whole word into the memory, ldb loads a byte, stw rA, 0(rB) stores a word. mov usually copies one register to another.

Upvotes: 1

Related Questions