Skip to content

Instantly share code, notes, and snippets.

@slayerlab
Last active January 27, 2018 19:00
Show Gist options
  • Save slayerlab/bb4e07cf26f73a978c512849d008f6bd to your computer and use it in GitHub Desktop.
Save slayerlab/bb4e07cf26f73a978c512849d008f6bd to your computer and use it in GitHub Desktop.
8-bits assembly code. It can be simulated for execution in: http://schweigi.github.io/assembler-simulator/index.html
JMP main ; jump to "main" label.
write: ; label "write".
DB "k33p h4ck1ng!" ; DB stands for "define byte" and allocates 1 byte.
DB 0 ; you NEED to declare another DB to put the null terminator \
; into the final string that goes on stdout.
main: ; label "main" - The "main" label is declared to be \
; the entry point
MOV C, write ; copies the buffer assigned in DB from "write" label to C register.
MOV D, 232 ; points D register to STDOUT.
CALL puts ; It is an CALLER! The CALLER performs "puts". \
; It means that CALL is calling the lable "puts".
; CALL is different to JMP-like because \
; the use of CALL causes it to return to the departure address. \
; the JMP can't rollback unless you force using another \
; JMP-like instructions.
HLT ; HLT means HALT = turn off [the execution program].
puts: ; the "puts" label.
PUSH A ; provides a variable (register A) in the stack that \
; has not yet been used.
PUSH B ; same thing as above (similar to register A).
MOV B, 0 ; assigning the value zero to "variable" [register] B.
.loop: ; The "." (dot) prefix denotes a "local symbol", \
; which is a symbol visible only in that source and \
; cannot be exported.
MOV A, [C] ; copies the current _ADDRESS OF_ C - which is the value of \
; each character, i.g: \
; ['k','3', '3', 'p', ' ', 'h', '4', 'c', 'k', '1', 'n', 'g']. \
; to register A.
MOV [D], A ; Now, A has the address of C that points to the current character.
; So, move it to STDOUT.
INC C ; Increment C. This make C to points to the next character.
INC D ; Increment D. This make D to points to the next STDOUT address, \
; avoid to overwrite the previous character echoed.
CMP B, [C] ; compare the value stored in register B to the value into \
; the address pointed to register C. Result expected is: B & C = 0.
JNZ .loop ; If the result compared between B & C is different to ZERO: \
; jump to local symbol ".loop". \
; Thus, by echoing each character in sequence, it will reach the \
; string that corresponds to the "final character" (null terminator: zero).
POP B ; If the result compared between B &C is ZERO: \
; remove the variable B from the stack.
POP A ; And, remove the variable A from the stack.
RET ; Finally, returns to the caller. \
; There is a difference between "caller" and "callee":
; CALLER is the one who perform the call.
; CALLEE is the one who is attracted by the call made by CALLER. \
; So, the RET (return) rollback to "main" label".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment