initial commit

This commit is contained in:
2019-02-19 22:02:06 +01:00
commit 59e78f996f
14 changed files with 405 additions and 0 deletions

10
.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# ASM object files
*.o
# Executables
tictactoe/tictactoe
tutorial/branching
tutorial/conditionaljump
tutorial/helloworld
tutorial/inputexample
tutorial/instructions

13
armv6/tictactoe/makefile Normal file
View File

@@ -0,0 +1,13 @@
AS = as
LD = ld
%.o: %.s
$(AS) $^ -o $@
tictactoe: tictactoe.o
$(LD) $^ -o $@
.PHONY: clean
clean:
rm tictactoe
rm *.o

195
armv6/tictactoe/tictactoe.s Normal file
View File

@@ -0,0 +1,195 @@
.global _start
_start:
BL _printboard
_inputloop:
BL _getdigit
LDR R1, =firstmove
STRB R0, [R1]
BL _getdigit
LDR R2, =firstmove
LDRB R1, [R2]
BL _isvalidmove
CMP R2, #1
BEQ _inputloop @ move was invalid, retry
BL _checkwin @ check if move was a winning one
CMP R0, #1
BEQ _player_won
LDR R1, =currentmove @ check if max moves exceeded
LDRB R2, [R1]
ADD R0, R2, #1
CMP R0, #8
BHI _tie_end @ max moves achieved, its a tie
STRB R0, [R1]
LDR R1, =currentplayer @ change active player
LDRB R0, [R1]
CMP R0, #79 @ '79' == 'O', player 1
BEQ _setplayer2
MOV R0, #79 @ set current player to 1
STRB R0, [R1]
B _start
_setplayer2:
MOV R0, #88 @ set current player to 2
STRB R0, [R1]
B _start
_player_won:
LDR R1, =currentplayer
LDRB R0, [R1]
LDR R1, =output_player_won
ADD R2, R1, #7
STRB R0, [R2] @ store the current player as winner
MOV R2, #14
BL _print
B _end
_tie_end:
LDR R1, =output_tie
MOV R2, #5
BL _print
_end:
BL _printboard
MOV R7, #1
SWI 0
_checkwin:
MOV R0, #0
_checkwin_iterate:
LDR R2, =boardlines @ base ptr
LDR R3, =board
ADD R1, R2, R0 @ iteration offset
LDRB R2, [R1], #1 @ index to check
ADD R4, R2, R3
LDRB R5, [R4] @ cell #1
CMP R5, #95 @ stop if first cell is empty
BEQ _checkwin_next_line
LDRB R2, [R1], #1
ADD R4, R2, R3
LDRB R6, [R4] @ cell #2
CMP R5, R6
BNE _checkwin_next_line
LDRB R2, [R1]
ADD R4, R2, R3
LDRB R7, [R4] @ cell #3
CMP R5, R7
BNE _checkwin_next_line
B _checkwin_win
_checkwin_next_line:
ADD R1, R0, #3
MOV R0, R1
CMP R0, #21
BLS _checkwin_iterate
_checkwin_nowin:
MOV R0, #0
BX lr
_checkwin_win:
MOV R0, #1
BX lr
_isvalidmove: @ checks if the move in R0, R1 is valid and sets R2 accordingly
MOV R3, #3
MUL R2, R1, R3
ADD R3, R0, R2
LDR R4, =board
ADD R2, R4, R3
LDRB R3, [R2]
CMP R3, #95 @ '95' == _ character, empty cell
BEQ _isvalidmove_succ
MOV R2, #1 @ error, cell already set, retry!
BX lr
_isvalidmove_succ:
LDR R3, =currentplayer @ store the move in the board
LDRB R4, [R3]
STRB R4, [R2]
MOV R2, #0 @ cell set
BX lr
_getdigit: @ Puts the first read char in R0
MOV R3, lr @ backup lr ptr
_getinput:
LDR R1, =inputreq
MOV R2, #6
BL _print
MOV R7, #3 @ syscall read
MOV R0, #0 @ from kb device
MOV R2, #2
LDR R1, =input @ into
SWI 0 @ interrupt to let sys handle the call
LDR R1, =input
LDRB R2, [R1]
CMP R2, #48 @ ascii value of '0'
BLS _getinput
CMP R2, #51 @ ascii value of '3'
BHI _getinput
SUB R0, R2, #49 @ subtract ascii value of '1' to bring result into range 0 to 2
MOV lr, R3 @ restore lr ptr
BX lr @ Return
_printboard:
MOV R10, lr
MOV R4, #0 @ row index (0, 3 or 6)
_printboard_row:
MOV R3, #0 @ value index (0, 1, 2)
_printboard_nextvalue:
ADD R1, R3, R4 @ calculate index
LDR R0, =board
ADD R2, R0, R1 @ get board array ptr
LDRB R0, [R2] @ load board byte
LDR R1, =output_row
ADD R2, R1, R3 @ get row output str ptr
STRB R0, [R2] @ store the player char in output str
ADD R0, R3, #1 @ up the value counter
MOV R3, R0
CMP R3, #2 @ check if we should print the row
BLS _printboard_nextvalue @ no, still 1 or more values to insert
LDR R1, =output_row @ prepare to print the row
MOV R2, #4
BL _print @ print the row
ADD R5, R4, #3 @ get next row
MOV R4, R5
CMP R4, #6 @ check if not on last row already
BHI _printboard_end @ printed all 3 rows, quit
B _printboard_row
_printboard_end:
BX R10
_print: @ prints the message pointed at by R1 with size in R2
MOV R7, #4 @ syscall write
MOV R0, #1 @ to the screen
SWI 0
BX lr
.data
currentplayer:
.byte 79
currentmove:
.byte 0x00
.align 9
board:
.byte 95, 95, 95, 95, 95, 95, 95, 95, 95
.align 24
boardlines:
.byte 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 3, 6, 1, 4, 7, 2, 5, 8, 0, 4, 8, 6, 4, 2 @ 8 x 3
firstmove:
.byte 0x00
endline:
.ascii "\n"
inputreq:
.ascii "Input:" @6
input:
.ascii " "
output_row:
.ascii " \n" @4
output_tie:
.ascii "tie!\n" @5
output_player_won:
.ascii "player X won!\n" @ size 14, X at index 7

View File

@@ -0,0 +1,12 @@
@ <intstruction> <destination> , <operand>, <operand>
.global _start
_start:
MOV R0, #20
B other
MOV R0, #11
other:
MOV R7, #1
SWI 0

View File

@@ -0,0 +1,32 @@
@ <intstruction> <destination> , <operand>, <operand>
.global _start
/*
CMP R1. R2
if R1 < R2 then N is enabled
if R1 > R2 then N is disabled
if R1 == R2 then Z is enabled
*/
_start:
MOV R1, #20
MOV R2, #10
CMP R1, R2
BEQ values_equal
BGT values_greater
values_less:
MOV R0, #2
B end
values_equal:
MOV R0, #1
B end
values_greater:
MOV R0, #3
end:
MOV R7, #1 @ exit to terminal
SWI 0 @ system reads R7 after interrupt

View File

@@ -0,0 +1,16 @@
.global _start
_start:
MOV R7, #4 @ system call to say we want to output
MOV R0, #1 @ use the window as output device
MOV R2, #12 @ length of our output message
LDR R1, =message
SWI 0
end:
MOV R7, #1
SWI 0
.data
message:
.ascii "Hello World\n"

View File

@@ -0,0 +1,23 @@
.global _start
_start:
MOV R7, #3 @ system call to say we want to read input
MOV R0, #0 @ read from input device keyboard
MOV R2, #10 @ read 10 chars
LDR R1, =message
SWI 0
_write:
MOV R7, #4 @ output
MOV R0, #1 @ to screen
MOV R2, #5 @ 5 chars
LDR R1, =message
SWI 0
end:
MOV R7, #1
SWI 0
.data
message:
.ascii " "

View File

@@ -0,0 +1,14 @@
@ <intstruction> <destination> , <operand>, <operand>
.global _start
_start:
MOV R1, #0x04
MOV R2, #0x03
MOV R3, #0x08
ADD R0, R1, R1
SUB R0, R1, #0x02
MUL R0, R1, R2
MLA R0, R1, R2, R3
MOV R7, #1
SWI 0

View File

@@ -0,0 +1,11 @@
@ instruction <destination>, <operand>, <operand>
.global _start
_start:
MOV R1, #5 @ 0101
MOV R2, #9 @ 1001
AND R0, R1, R2
end:
MOV R7, #1
SWI 0

34
armv6/tutorial/makefile Normal file
View File

@@ -0,0 +1,34 @@
AS = as
LINKER = ld
%.o: %.s
$(AS) $^ -o $@
tutorial: tutorial.o
$(LINKER) $^ -o $@
tutorial2: tutorial2.o tutorial2p2.o
$(LINKER) $^ -o $@
helloworld: helloworld.o
$(LINKER) $^ -o $@
inputexample: inputexample.o
$(LINKER) $^ -o $@
instructions: instructions.o
$(LINKER) $^ -o $@
branching: branching.o
$(LINKER) $^ -o $@
conditionaljump: conditionaljump.o
$(LINKER) $^ -o $@
.PHONY: clean
clean:
rm *.o
rm tutorial
rm tutorial2
rm helloworld
rm inputexample

15
armv6/tutorial/tutorial.s Normal file
View File

@@ -0,0 +1,15 @@
@single line comment
/*
Multi line comment
*/
.text
.global _start @entry point
_start: @label
MOV R0, #65
MOV R7, #1
SWI 0

View File

@@ -0,0 +1,4 @@
.global _start
_start:
MOV R0, #65
BAL _part2

View File

@@ -0,0 +1,4 @@
.global _part2
_part2:
MOV R7, #1
SWI 0

View File

@@ -0,0 +1,22 @@
/*
bit = 1 bit
nibble = 4 bits
byte = 8 bits
word = 32 bits
*/
/*
16 registers of length word
13 general purpose
1 stack pointer
1 link register
1 program counter
*/
/*
CPSR = Current Program Status Registers
N = negative result flag
Z = zero result flag
C = carry over flag
V = overflow flag
*/