diff --git a/Assembly-program-examples.md b/Assembly-program-examples.md new file mode 100644 index 0000000..f33abe2 --- /dev/null +++ b/Assembly-program-examples.md @@ -0,0 +1,80 @@ + +### Manhattan Distance +This simple program calculates the [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) between the current position of the Cubot and another tile. +```assembly +; Define constants +LIDAR_HW equ 0x0003 ; HWID of the LiDAR +HOLO_HW equ 0x0009 ; HWID of the Hologram projector + +; Data segment +.data + +counter: DW 0x0000 ; Counter global variable +; The DW (Define word) directive writes the specified value(s), seperated by a +; comma at assembly time. You can define a label before the directive to refer +; to it by its name. + +; Code/executable segment +.text + ; Program entry point + call getPos ; Execute procedure + + PUSH 7 ; y2 + PUSH 11 ; x2 + call manhattanDist ; Execute procedure + + TEST [counter], 1 ; Display 'ABCD' every other tick + JNZ counter_is_odd + MOV A, 0xABCD +counter_is_odd: + + ADD [counter], 1 ; Increment counter + HWI HOLO_HW ; Display the contents of A + BRK ; Exit program + +;************************************** +;* getPos() +;* X = X pos +;* Y = Y pos +getPos: + PUSH A ; Save A + MOV A, 0x0001 ; GET_POS + HWI LIDAR_HW ; Send hardware interrupt to the LiDAR + POP A ; Restore A + RET + + +;************************************** +;* manhattanDist(x2,y2) +;* A = Distance between (X,Y) and (x2,y2) +manhattanDist: + PUSH BP ; Save previous stack frame + MOV BP, SP + + ; The old value of BP is at [BP + 0] + ; The return address is at [BP + 1] + ; The x2 argument is at [BP + 2] + ; The y2 argument is at [BP + 3] + + ; formula: |x1-x2| + |y1-y2| + SUB X, [BP + 2] ; X = (X - x2) + + ; A fancy way to get the absolute value of X + MOV A, X ; Copy X + SAR A, 15 ; Fill A with sign + XOR X, A ; Do 'NOT X' if negative + SUB X, A ; Do 'ADD X, 1' if negative + + SUB Y, [BP + 3] ; Y = (Y - y2) + ; A simpler way to get the absolute value of Y + JNS y_is_positive + NEG Y ; If Y was negative, it is NEG'd +y_is_positive: + + MOV A, X ; A = |x1-x2| + ADD A, Y ; A = |x1-x2| + |y1-y2| + + POP BP ; Restore the previous stack frame + RET 2 ; Return and POP our 2 arguments + +``` \ No newline at end of file