Skip to content

Instantly share code, notes, and snippets.

@ammrat13
Last active August 6, 2020 21:21
Show Gist options
  • Save ammrat13/c567637d1cee2ea6248567d90a634dbe to your computer and use it in GitHub Desktop.
Save ammrat13/c567637d1cee2ea6248567d90a634dbe to your computer and use it in GitHub Desktop.
;; Start common.asm
;;
;; A file containing macros that may be useful to include. Mostly contains
;; basic operations that would be included on a modern architecture.
; REGISTER DEFINITIONS: ----------
#ifnc LR
#constant LR R7
#endif
#ifnc SP
#constant SP R6
#endif
#ifnc FP
#constant FP R5
#endif
; STACK OPERATIONS: ----------
; Push
#ifnm PUSH
#macro PUSH RX
ADD R6, R6, #-1
STR RX, R6, #0
#endmacro
#endif
; Pop
#ifnm POP
#macro POP RX
LDR RX, R6, #0
ADD R6, R6, #1
#endmacro
#endif
; MATH AND LOGIC: ----------
; In-Place Add
; RA <- RA + P2
#ifnm ADDP
#macro ADDP RA, P2
ADD RA, RA, P2
#endmacro
#endif
; In-Place And
; RA <- RA & P2
#ifnm ANDP
#macro ANDP RA, P2
AND RA, RA, P2
#endmacro
#endif
; In-Place Not
; RX <- ~RX
#ifnm NOTP
#macro NOTP RX
NOT RX, RX
#endmacro
#endif
; Negation
; RD <- -RX
#ifnm NEG
#macro NEG RD, RX
NOT RD, RX
ADD RD, RD, #1
#endmacro
#endif
; Clear
; RD <- 0
#ifnm CLR
#macro CLR RD
AND RD, RD, #0
#endmacro
#endif
; Test
; Does nothing to the registers, but sets condition codes
#ifnm TST
#macro TST RX
ADD RX, #0
#endmacro
#endif
; Move
; Moves the specified immediate or register into the destination
#ifnm MOV
#macro MOV RD, P2
AND RD, RD, #0
ADD RD, RD, P2
#endmacro
#endif
; In-Place Subtraction
; Note that there is no arbitrary-destination subtraction. Doing that requires
; executing different code based on if RD is RA or RB
; We can ignore the edge case of RB being the same as RA. The code results in
; 0 in RA, as it should
; RA <- RA - RB
#ifnm SUBP
#macro SUB RA, RB
NOT RB, RB
ADD RB, RB, #1
ADD RA, RA, RB
NOT RB, RB
ADD RB, RB, #1
#endmacro
#endif
; In-Place Or
; For the same reason as above, there is no arbitrary-destination or.
; Also, like above, the code still works
; RA <- RA | RB
#ifnm ORP
#macro ORP RA, RB
NOT RA, RA
NOT RB, RB
AND RA, RA, RB
NOT RA, RA
NOT RB, RB
#endmacro
#endif
;; End common.asm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment