Skip to content

Instantly share code, notes, and snippets.

@randrews
Created August 11, 2022 05:20
Show Gist options
  • Save randrews/8b777aa55566d01b58a9029d0614732f to your computer and use it in GitHub Desktop.
Save randrews/8b777aa55566d01b58a9029d0614732f to your computer and use it in GitHub Desktop.
Three arguments: dest, src, mode
Mode is a word of three bytes, separated into three one-byte sub-args
The sub-args are:
- High byte: mode arg
- Mid / low bytes are a 16-bit limit arg
- (Some end modes only use the low byte)
Mode arg is three fields:
- how the src is interpreted / incremented
- how the dest is interpreted / incremented
- how the end of the loop is triggered
src modes: (000000xx)
- src is address, increment (00)
- src is address, decrement (01)
- src is address, constant (10)
- src is literal (11)
dest modes: (0000xx00)
- dest is address, increment (00)
- dest is address, decrement (01)
- dest is address, constant (10)
- dest is ignored, do not copy (11)
end modes: (00xx0000)
- Count: iterate limit times (00)
- Until: iterate until src equals end arg (low byte) (01)
- While-greater: iterate while src is greater than end arg (low byte) (10)
- While-less: iterate while src is less than end arg (low byte) (11)
Return values:
- Mode is consumed
- Both src and dest args are left on the stack...
- But in the state they were in when the end test failed
Examples:
- Copy 16 bytes from 0x1000 to 0x1000:
push 0x2000
push 0x1000
copy 16 ; leaves 0x2010, 0x1010 on the stack; last byte copied was 0x100f
- Copy from 0x1000 to 0x2000 until a null terminator:
push 0x2000
push 0x1000
copy 0x100000
; Or until a space char (32):
copy 0x100020
- Find the first 0 after 0x1000 (strlen):
push 0x1000 ; Don't need a dest since it'll be ignored anyway
copy 0x1c0000 ; leaves the address of the 0 on the stack
- Clear an array of 64 bytes after 0x1000 (memset)
push 0x1000
push 32 ; what we'll clear it to
copy 0x030040 ; end mode: count, dest mode: increment, src mode: literal, mode arg: 64
- Copy each byte of a null-termed string to 0x41 (puts):
push 0x41
push 0x1000
copy 0x180000 ; end mode: until, dest mode: constant, src mode: increment, mode arg: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment