views:

59

answers:

2

hello, can somebody please explain what this program is doing?

.= torg + 1000

main:
        mov pc, sp
        tst –(sp)

        mov #list1, -(sp)
        jsr pc, mystery
        mov r0, res1
        tst (sp)+

        mov #list2, -(sp)
        jsr pc, mystery
        mov r0, res2
        tst (sp)+

        halt


mystery:
        mov r1, -(sp)
        mov r4, -(sp)
        mov r5, -(sp)

        clr r0

        mov 10(sp), r4
        mov r4, r5

loop:
        mov r4, r1
        jsr pc, next
        mov r1, r4
        beq return

        mov r5, r1
        jsr pc, next
        jsr pc, next
        mov r1, r5
        beq return

        cmp r4, r5
        beq setret
        br loop

setret:
        inc r0

return:
        mov (sp)+, r5
        mov (sp)+, r4
        mov (sp)+, r1
        rts pc


next:
        tst r1
        beq abort
        mov (r1), r1
abort:
        rts pc


.= torg + 3000
list1: .word 3006, 3000, 3002, 3004, 0
res1: .word -1

.= torg + 3020
list2: .word 3030, 3026, 0, 3024, 3022
res2: .word -1

I can't understand this snippet, thanks in advance for everyone

mystery:
            mov r1, -(sp)
            mov r4, -(sp)
            mov r5, -(sp)

            clr r0

            mov 10(sp), r4
            mov r4, r5
A: 

It appears to be backing up registers 1, 4, and 5 and initializing register 0 (which doesn't need to be backed up). Since @mystery is the destination of a jsr, this is called prologue code. Then, they are initialized for the loop.

The old values are restored at @return.

As for what the whole program does, it appears to find cyclic links in a linked list.

bool is_invalid_list( link_node *l ) {
    while ( l && l->next && l->next->next ) {
        if ( l->next == l->next->next ) return true;
    }
    return false;
}

I don't think this is the simplest or best way to implement this, but not the worst either.

Potatoswatter
@Potatoswatter: explain please in this row mov #list1, -(sp) do I send pointer to list1 or all values from list1?
lego69
@lego: just the pointer, that is, the value of the label `list1`. To the processor, that is just a number like any other.
Potatoswatter
and one more question: why do we need this rowsmov r1, -(sp)mov r4, -(sp)mov r5, -(sp)we didn't use these registers
lego69
@lego: See the Wikipedia article I linked and the articles it links to. By convention (formalized by the ABI of any platform), most registers need to be backed up and restored when used by a function so that any calling functions can use them irrespectively.
Potatoswatter
A: 
        mov r1, -(sp)
        mov r4, -(sp)
        mov r5, -(sp)

This is pushing the three registers onto the stack.

        clr r0

Obvious.

        mov 10(sp), r4
        mov r4, r5

This retrieves a parameter off the stack into r4 (and then copies it to r5).

Jerry Coffin
can You explain please, what this program is doing?
lego69
and how this mov 10(sp), r4 works?
lego69
@lego69: it's loading the word from an offset of 10 from (sp) into r4. That'll be 5 words up the stack, and since we just pushed three words onto the stack, it's the second parameter that was passed in.
Jerry Coffin
@Jerry: only one parameter gets passed. `jsr` probably pushes the PC.
Potatoswatter
@potatoswatter:oops, quite right.
Jerry Coffin