From 59e78f996f98f1e74d4f14bb357be5d3d5455aef Mon Sep 17 00:00:00 2001 From: Tijmen van Nesselrooij Date: Tue, 19 Feb 2019 22:02:06 +0100 Subject: [PATCH] initial commit --- .gitignore | 10 ++ armv6/tictactoe/makefile | 13 ++ armv6/tictactoe/tictactoe.s | 195 ++++++++++++++++++++++++++++++ armv6/tutorial/branching.s | 12 ++ armv6/tutorial/conditionaljump.s | 32 +++++ armv6/tutorial/helloworld.s | 16 +++ armv6/tutorial/inputexample.s | 23 ++++ armv6/tutorial/instructions.s | 14 +++ armv6/tutorial/logicaloperators.s | 11 ++ armv6/tutorial/makefile | 34 ++++++ armv6/tutorial/tutorial.s | 15 +++ armv6/tutorial/tutorial2.s | 4 + armv6/tutorial/tutorial2p2.s | 4 + armv6/tutorial/tutorial3.s | 22 ++++ 14 files changed, 405 insertions(+) create mode 100644 .gitignore create mode 100644 armv6/tictactoe/makefile create mode 100644 armv6/tictactoe/tictactoe.s create mode 100644 armv6/tutorial/branching.s create mode 100644 armv6/tutorial/conditionaljump.s create mode 100644 armv6/tutorial/helloworld.s create mode 100644 armv6/tutorial/inputexample.s create mode 100644 armv6/tutorial/instructions.s create mode 100644 armv6/tutorial/logicaloperators.s create mode 100644 armv6/tutorial/makefile create mode 100644 armv6/tutorial/tutorial.s create mode 100644 armv6/tutorial/tutorial2.s create mode 100644 armv6/tutorial/tutorial2p2.s create mode 100644 armv6/tutorial/tutorial3.s diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75152c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# ASM object files +*.o + +# Executables +tictactoe/tictactoe +tutorial/branching +tutorial/conditionaljump +tutorial/helloworld +tutorial/inputexample +tutorial/instructions diff --git a/armv6/tictactoe/makefile b/armv6/tictactoe/makefile new file mode 100644 index 0000000..1faad18 --- /dev/null +++ b/armv6/tictactoe/makefile @@ -0,0 +1,13 @@ +AS = as +LD = ld + +%.o: %.s + $(AS) $^ -o $@ + +tictactoe: tictactoe.o + $(LD) $^ -o $@ + +.PHONY: clean +clean: + rm tictactoe + rm *.o diff --git a/armv6/tictactoe/tictactoe.s b/armv6/tictactoe/tictactoe.s new file mode 100644 index 0000000..da690cc --- /dev/null +++ b/armv6/tictactoe/tictactoe.s @@ -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 diff --git a/armv6/tutorial/branching.s b/armv6/tutorial/branching.s new file mode 100644 index 0000000..4aa9617 --- /dev/null +++ b/armv6/tutorial/branching.s @@ -0,0 +1,12 @@ +@ , , + +.global _start + +_start: + MOV R0, #20 + B other + MOV R0, #11 + +other: + MOV R7, #1 + SWI 0 diff --git a/armv6/tutorial/conditionaljump.s b/armv6/tutorial/conditionaljump.s new file mode 100644 index 0000000..6dd60ae --- /dev/null +++ b/armv6/tutorial/conditionaljump.s @@ -0,0 +1,32 @@ +@ , , + +.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 diff --git a/armv6/tutorial/helloworld.s b/armv6/tutorial/helloworld.s new file mode 100644 index 0000000..109404e --- /dev/null +++ b/armv6/tutorial/helloworld.s @@ -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" diff --git a/armv6/tutorial/inputexample.s b/armv6/tutorial/inputexample.s new file mode 100644 index 0000000..18202b6 --- /dev/null +++ b/armv6/tutorial/inputexample.s @@ -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 " " diff --git a/armv6/tutorial/instructions.s b/armv6/tutorial/instructions.s new file mode 100644 index 0000000..6f31463 --- /dev/null +++ b/armv6/tutorial/instructions.s @@ -0,0 +1,14 @@ +@ , , + +.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 diff --git a/armv6/tutorial/logicaloperators.s b/armv6/tutorial/logicaloperators.s new file mode 100644 index 0000000..a5aea94 --- /dev/null +++ b/armv6/tutorial/logicaloperators.s @@ -0,0 +1,11 @@ +@ instruction , , + +.global _start + +_start: + MOV R1, #5 @ 0101 + MOV R2, #9 @ 1001 + AND R0, R1, R2 +end: + MOV R7, #1 + SWI 0 diff --git a/armv6/tutorial/makefile b/armv6/tutorial/makefile new file mode 100644 index 0000000..8788359 --- /dev/null +++ b/armv6/tutorial/makefile @@ -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 diff --git a/armv6/tutorial/tutorial.s b/armv6/tutorial/tutorial.s new file mode 100644 index 0000000..8faf4bd --- /dev/null +++ b/armv6/tutorial/tutorial.s @@ -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 diff --git a/armv6/tutorial/tutorial2.s b/armv6/tutorial/tutorial2.s new file mode 100644 index 0000000..07b48b3 --- /dev/null +++ b/armv6/tutorial/tutorial2.s @@ -0,0 +1,4 @@ +.global _start +_start: + MOV R0, #65 + BAL _part2 diff --git a/armv6/tutorial/tutorial2p2.s b/armv6/tutorial/tutorial2p2.s new file mode 100644 index 0000000..655335e --- /dev/null +++ b/armv6/tutorial/tutorial2p2.s @@ -0,0 +1,4 @@ +.global _part2 +_part2: + MOV R7, #1 + SWI 0 diff --git a/armv6/tutorial/tutorial3.s b/armv6/tutorial/tutorial3.s new file mode 100644 index 0000000..9d4ad69 --- /dev/null +++ b/armv6/tutorial/tutorial3.s @@ -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 +*/