/ Low Level

My first program in ARM assembly

I made a crappy tic-tac-toe game in ARM:

Basically you enter in a number 1 - 9 which corresponds with one of the spots on the field, X starts first and it alternates.

The game ends with "X wins!" or "O wins!" or "Tied."

.global main

_putchar:  
    ldr r3, =STDOUT
    ldr r1, [r3]
    push {lr}
    bl putchar
    pop {lr}
    bx lr

printmap:  
    push {r4, r5}
    mov r5, #3
printmap_0:  
    mov r4, #3
printmap_1:  
    tst r0, #1
    push {r0, r1, lr}
    mov r0, #0x4F
    moveq r0, #32
    tst r1, #1
    movne r0, #0x58
    bl _putchar
    pop {r0, r1, lr}
    lsr r0, r0, #1
    lsr r1, r1, #1
    subs r4, r4, #1
    bne printmap_1
    push {r0, r1, lr}
    mov r0, #0xA
    bl _putchar
    pop {r0, r1, lr}
    subs r5, r5, #1
    bne printmap_0
    pop {r4, r5}
    bx lr

.winmaps:
    .word 0x007
    .word 0x038
    .word 0x1C0
    .word 0x124
    .word 0x092
    .word 0x049
    .word 0x111
    .word 0x054
    .word 0
checkwin:  
    ldr r1, =.winmaps
checkwin_loop:  
    ldr r2, [r1]
    cmp r2, #0
    moveq r0, #0
    bxeq lr
    bics r2, r2, r0
    beq checkwin_win
    add r1, r1, #4
    b checkwin_loop
checkwin_win:  
    mov r0, #1
    bx lr

readnum:  
    push {lr}
    bl getchar
    pop {lr}
    cmp r0, #0x31
    blt readnum
    cmp r0, #0x39
    bgt readnum
    sub r0, r0, #0x30
    bx lr

main:  
    push {r4, r5, r6, r8, r9, lr}
    mov r4, #0x00
    mov r5, #0x00
    mov r8, #0
    ldr r0, =.hello
    bl puts
loop:  
    mov r0, r4
    mov r1, r5
    bl printmap

    orr r0, r4, r5
    ldr r1, =511
    cmp r0, r1
    beq tied

    mov r0, r4
    bl checkwin
    cmp r0, #0
    movne r0, #0
    bne winner
    mov r0, r5
    bl checkwin
    cmp r0, #0
    movne r0, #1
    bne winner

    bl readnum
    sub r0, r0, #1

    mov r1, #1
    lsl r1, r1, r0

    orr r0, r4, r5
    ands r0, r0, r1
    bne invalid

    eors r8, r8, #1
    orreq r4, r4, r1
    orrne r5, r5, r1

    b loop
invalid:  
    ldr r0, =.invalid
    bl puts
    b loop
winner:  
    cmp r0, #0
    moveq r0, #0x4F
    movne r0, #0x58
    bl putchar
    ldr r0, =.wins
    bl puts
    b endgame
tied:  
    ldr r0, =.tied
    bl puts
endgame:  
    pop {r4, r5, r6, r8, r9, lr}
    mov r0, #0
    bx lr

.hello:
    .string "Hello!"
.invalid:
    .string "Invalid move."
.tied:
    .string "Tied."
.wins:
    .string " wins!"
STDOUT:  
    .word stdout
My first program in ARM assembly
Share this