Skip to topic | Skip to bottom
Bennington College
r1.6 - 27 Feb 2006 - 21:10 - JoeHolt

Start of topic | Skip to actions
; This program moves a ball back and forth on an imaginary screen,
; bouncing it off the left and right boundaries.

Addr Code    Instruction         Comment
00   1305    MOV  R0, #5         ; x  - start the ball in the middle
01   1701    MOV  R1, #1         ; dx - initially moving to the right

02   2000    MOV  A, R0          ; x = x + dx
03   2500    MOV  B, R1
04   4000    ADD  R0
05   2000    MOV  A, R0          ; hit the left boundary?
06   3700    MOV  B, #0
07   4D00    CMP
08   FC01    BEQ  01             ; yes, begin moving to the right

09   3709    MOV  B, #9          ; hit the right boundary?
0A   4D00    CMP
0B   F802    BNE  02             ; nope

0C   17FF    MOV  R1, #-1        ; yes, begin moving to the left
0D   F002    BRA  02


The equivalent code in C might look like this:

    int x, dx;

    x = 5;
    dx = 1;

    while (1) {
        x += dx;
        if (x == 0)
            dx = 1;
        else if (x == 9)
            dx = -1;
    }