mirror of
				https://github.com/simon987/Much-Assembly-Required.git
				synced 2025-11-04 01:56:53 +00:00 
			
		
		
		
	
						
							Page:
							Assembly program examples
						
						
					
					
							Pages
							
							
								(Hardware) Battery
							
								(Hardware) Clock
							
								(Hardware) Construction Arm
							
								(Hardware) Core
							
								(Hardware) Drill
							
								(Hardware) Floppy Drive
							
								(Hardware) Hologram Projector
							
								(Hardware) Inventory
							
								(Hardware) Keyboard
							
								(Hardware) Laser
							
								(Hardware) Legs
							
								(Hardware) LiDAR
							
								(Hardware) Radio Receiver
							
								(Hardware) Random Number Generator
							
								(Hardware) Universal Com Port
							
								(Installation) Arch Linux
							
								(Installation) Windows
							
								(Snippet) Constants
							
								(Snippet) Dump memory to disk
							
								(Snippet) Keyboard controlled robot
							
								(Snippet) Manhattan distance
							
								(Snippet) Move in a random direction
							
								Assembler directives
							
								Assembly program examples
							
								Basic Assembly tutorial
							
								Biomass
							
								Blueprint
							
								CPU
							
								Collaboration Guide
							
								Day Night Cycle and solar Energy
							
								Debugging
							
								Game Universe
							
								Hacked NPC
							
								Hardware
							
								Home (old)
							
								Home
							
								Instruction Encoding
							
								Instruction Set
							
								Items
							
								Learn by Examples
							
								NPC Factory
							
								Non Player Character (NPC)
							
								Project structure
							
								Radio Tower
							
								Random Access Memory
							
								Road Map
							
								The Cubot Manuals
							
								The Game Universe
							
								Useful Code Snippets
							
								Users' Repositories
							
								World
							
						
					 Clone
	
					14 
					
				
						Assembly program examples
						
				
							
							Arthur Paulino edited this page 2018-01-01 12:54:34 -03:00 
						
					Manhattan Distance
This simple program calculates the Manhattan distance between the current position of the Cubot and another tile.
; 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 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 odd tick
    JNZ counter_is_odd
    MOV A, 0xABCD
counter_is_odd:
    ADD [counter], 1    ; Increment counter
    MOV B, A
    MOV A, 1
    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
Dump memory to disk
HWID_FLOPPY   equ 0xB
;**************************************
;* dumpMemToDisk()
;* Dump entire memory to Floppy Disk     
dumpMemToDisk:
    MOV X, 0
    MOV Y, 0
dumpMemToDiskLoop:
    MOV A, 3 ; WRITE_SECTOR
    HWI HWID_FLOPPY
    ADD X, 1
    ADD Y, 512
    CMP X, 128
    JNZ dumpMemToDiskLoop
    RET
Constants by hackersoup with help from contributors
Insert this code before the new code you write for MuchAssemblyRequired, and use a text editor like VSCode, Notepad++, Sublime, etc. for auto-complete.
;*************************** Hardware IDs
HWID_LEGS     equ 0x1
HWID_LASER    equ 0x2
HWID_LIDAR    equ 0x3
HWID_KEYBOARD equ 0x4
HWID_DRILL    equ 0x5
HWID_INV      equ 0x6
HWID_RNG      equ 0x7
HWID_CLOCK    equ 0x8
HWID_HOLO     equ 0x9
HWID_BATTERY  equ 0xA
HWID_FLOPPY   equ 0xB
;*************************** Drill actions IDs
DRILL_POLL        equ 1
DRILL_GATHER_SLOW equ 2
DRILL_GATHER_FAST equ 3
; Drill status
DRILL_STATUS_OK   equ 0
DRILL_STATUS_BUSY equ 1
;*************************** Inventory actions IDs
INV_POLL  equ 1
INV_CLEAR equ 2
INV_EMPTY equ 0
;*************************** Laser actions IDs
LASER_WITHDRAW equ 1
LASER_DEPOSIT  equ 2
; Items that can be used with the Laser
ITEM_BIOMASS   equ 0x1
;*************************** Legs actions IDs
LEGS_SET_DIRECTION          equ 1
LEGS_SET_DIRECTION_AND_WALK equ 2
; Legs directions
LEGS_DIR_NORTH equ 0
LEGS_DIR_EAST  equ 1
LEGS_DIR_SOUTH equ 2
LEGS_DIR_WEST  equ 3
;*************************** LiDAR actions IDs
LIDAR_GET_POS       equ 1
LIDAR_GET_PATH      equ 2
LIDAR_GET_MAP       equ 3
LIDAR_GET_WORLD_POS equ 4
;*************************** Keyboard actions IDs
KEYBOARD_CLEAR     equ 0
KEYBOARD_FETCH_KEY equ 1
; Keys to use with Keyboard Hardware
KEY_A              equ 0x41
KEY_D              equ 0x44
KEY_E              equ 0x45
KEY_F              equ 0x46
KEY_S              equ 0x53
KEY_W              equ 0x57
;*************************** Hologram Projector actions IDs
HOLO_CLEAR          equ 0
HOLO_DISPLAY_HEX    equ 1
HOLO_DISPLAY_STRING equ 2
HOLO_DISPLAY_DEC    equ 3
HOLO_DISPLAY_COLOR  equ 4
;*************************** Battery actions IDs
BATTERY_POLL             equ 1
BATTERY_GET_MAX_CAPACITY equ 2
;*************************** Random Number Generator actions IDs
RNG_POLL equ 0
;*************************** Clock actions IDs
CLOCK_POLL equ 0
;*************************** Floppy Disk actions IDs
FLOPPY_POLL         equ 1
FLOPPY_READ_SECTOR  equ 2
FLOPPY_WRITE_SECTOR equ 3
Example
; paste constants definitions here
; the rest of your code goes like
.text
    CALL print_battery
;**************************************
;* print_battery()
;* display / print remaing battery charge
;* does not require arguments from stack or register prior to calling
;* does not overwrite or alter stack or registers
print_battery:
    PUSH A ; store A
    PUSH B ; store B
    MOV A, BATTERY_POLL ; indicates we want to poll the battery charge when
                        ; interrupt sent to battery
    HWI HWID_BATTERY    ; battery remaining charge (kilo Joules)
                        ; is stored in B register now
    MOV A, HOLO_DISPLAY_HEX ; HOLO_DISPLAY_HEX == BATTERY_POLL == 1
                            ; makes this redundant, but you get the idea.
    HWI HWID_HOLO           ; value in B register is displayed on your bot
    POP B
    POP A
    RET