Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions asm/tiny16.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ static char* preprocess_source(const char* filename);
int main(int argc, char** argv) {
make_and_parse_args(argc, argv);

//
// Pass 0: Preprocess (expand macros and includes)
//

char* source_content = preprocess_source(args.source_filename);
if (source_content == NULL) {
Expand Down Expand Up @@ -72,9 +70,7 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

//
// Pass 1: Collect labels and parse data section
//

parser.lexer = lexer_new(source_content, strlen(source_content));
tiny16_parser_next(&parser);
Expand Down Expand Up @@ -141,9 +137,7 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

//
// Pass 2: Emit code section
//

parser.current_section = TINY16_PARSER_SECTION_CODE;
parser.code_pc = TINY16_MEMORY_CODE_BEGIN;
Expand Down
7 changes: 2 additions & 5 deletions asm/tutorial/01_load_and_halt.asm
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
; Tutorial 01: Load and Halt
; Level: 1 - Fundamentals
;
; Goal: Load the value 42 into register R0, then halt the program
;
; Instructions to use: LOADI, HALT
; Expected result: R0 = 42
;
; Hint: LOADI R, imm8 loads an 8-bit immediate value into a register
; Hint: HALT stops program execution

section .code

loadi r0, 42 ; Load 42 into R0
halt ; Halt the program
loadi r0, 42
halt
11 changes: 4 additions & 7 deletions asm/tutorial/02_add_two_numbers.asm
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
; Tutorial 02: Add Two Numbers
; Level: 1 - Fundamentals
;
; Goal: Load 7 into R0, load 8 into R1, then add them (result = 15 in R0)
;
; Instructions to use: LOADI, ADD, HALT
; Expected result: R0 = 15, R1 = 8
;
; Hint: ADD R1, R2 means R1 = R1 + R2 (result goes in first register)
; Hint: ADD sets the Z flag if result is 0, C flag if result overflows

section .code

loadi r0, 7 ; Load 7 into R0
loadi r1, 8 ; Load 8 into R1
add r0, r1 ; Add R1 to R0 (R0 = R0 + R1)
halt ; Halt the program
loadi r0, 7
loadi r1, 8
add r0, r1
halt
9 changes: 3 additions & 6 deletions asm/tutorial/03_copy_register.asm
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
; Exercise 03: Copy Register
; Level: 1 - Fundamentals
;
; Goal: Load 25 into R0, then copy it to R1 using MOV
;
; Instructions to use: LOADI, MOV, HALT
; Expected result: R0 = 25, R1 = 25
;
; Hint: MOV R1, R2 means R1 = R2 (copy R2 to R1)
; Hint: MOV does not affect flags or modify R2

section .code

loadi r0, 25 ; Load 25 into R0
mov r1, r0 ; Copy R0 to R1
halt ; Halt the program
loadi r0, 25
mov r1, r0
halt
11 changes: 4 additions & 7 deletions asm/tutorial/04_simple_subtraction.asm
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
; Exercise 04: Simple Subtraction
; Level: 1 - Fundamentals
;
; Goal: Load 15 into R0, load 10 into R1, then subtract (R0 = R0 - R1 = 5)
;
; Instructions to use: LOADI, SUB, HALT
; Expected result: R0 = 5, R1 = 10
;
; Hint: SUB R1, R2 means R1 = R1 - R2 (result goes in first register)
; Hint: SUB sets C flag if borrow occurs (R1 < R2)

section .code

loadi r0, 15 ; Load 15 into R0
loadi r1, 10 ; Load 10 into R1
sub r0, r1 ; Subtract R1 from R0 (R0 = R0 - R1)
halt ; Halt the program
loadi r0, 15
loadi r1, 10
sub r0, r1
halt
12 changes: 4 additions & 8 deletions asm/tutorial/05_increment_decrement.asm
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
; Exercise 05: Increment and Decrement
; Level: 1 - Fundamentals
;
; Goal: Load 10 into R0, increment it twice, then decrement it once
;
; Instructions to use: LOADI, INC, DEC, HALT
; Expected result: R0 = 11
;
; Hint: INC R increments register by 1 (R = R + 1)
; Hint: DEC R decrements register by 1 (R = R - 1)
; Hint: INC and DEC set Z flag if result is 0, but always clear C flag

section .code

loadi r0, 10 ; Load 10 into R0
times 2 inc r0 ; Increment R0 (R0 becomes 11)
; Increment R0 again (R0 becomes 12)
dec r0 ; Decrement R0 (R0 becomes 11)
halt ; Halt the program
loadi r0, 10
times 2 inc r0
dec r0
halt
9 changes: 3 additions & 6 deletions asm/tutorial/06_zero_register.asm
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
; Exercise 06: Zero a Register
; Level: 1 - Fundamentals
;
; Goal: Load any value into R0, then set it to 0 using XOR (not using LOADI R0, 0)
;
; Instructions to use: LOADI, XOR, HALT
; Expected result: R0 = 0
;
; Hint: XOR R, R always equals 0 (any value XOR itself = 0)
; Hint: This is a common assembly idiom to zero a register
; Hint: XOR sets Z=1 (because result is 0) and clears C flag

section .code

loadi r0, 99 ; Load any non-zero value into R0 (e.g., 99)
xor r0, r0 ; XOR R0 with itself to zero it
halt ; Halt the program
loadi r0, 99
xor r0, r0
halt
9 changes: 3 additions & 6 deletions asm/tutorial/07_double_number.asm
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
; Exercise 07: Double a Number
; Level: 1 - Fundamentals
;
; Goal: Load 10 into R0, then double it to 20
;
; Instructions to use: LOADI, SHL (or ADD), HALT
; Expected result: R0 = 20
;
; Hint: You can double a number two ways:
; 1. SHL R0 (shift left = multiply by 2)
; 2. ADD R0, R0 (add to itself)
; Hint: SHL sets C flag if bit 7 was 1 before shift

section .code

loadi r0, 10 ; Load 10 into R0
shl r0 ; Double R0 using SHL or ADD
halt ; Halt the program
loadi r0, 10
shl r0
halt
9 changes: 3 additions & 6 deletions asm/tutorial/08_halve_number.asm
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
; Exercise 08: Halve a Number
; Level: 1 - Fundamentals
;
; Goal: Load 20 into R0, then halve it to 10
;
; Instructions to use: LOADI, SHR, HALT
; Expected result: R0 = 10
;
; Hint: SHR R (shift right) divides by 2 (rounds down)
; Hint: SHR sets C flag if bit 0 was 1 before shift (odd number)

section .code

loadi r0, 20 ; Load 20 into R0
shr r0 ; Halve R0 using SHR (shift right)
halt ; Halt the program
loadi r0, 20
shr r0
halt
9 changes: 3 additions & 6 deletions asm/tutorial/09_unconditional_jump.asm
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
; Exercise 09: Unconditional Jump
; Level: 2 - Control Flow
;
; Goal: Create an infinite loop using JMP and a label
; Load different values in each iteration to show it's looping
;
; Instructions to use: LOADI, JMP, INC, HALT
; Expected result: Infinite loop (will need -m flag to limit execution)
;
; Hint: Labels are defined with a colon: loop:
; Hint: JMP label jumps unconditionally to that label
; Hint: Use the emulator with -m 10 to limit to 10 instructions

section .code

loadi r0, 0 ; Load 0 into R0
loadi r0, 0

loop:
inc r0 ; Increment R0
jmp loop ; Jump back to loop
inc r0
jmp loop
; Note: This program never reaches HALT - that's the point!
11 changes: 4 additions & 7 deletions asm/tutorial/10_simple_loop.asm
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
; Exercise 10: Simple Loop
; Level: 2 - Control Flow
;
; Goal: Count down from 10 to 0 using a loop
; Use DEC and JNZ to repeat while R0 is not zero
;
; Instructions to use: LOADI, DEC, JNZ, HALT
; Expected result: R0 = 0 (after counting down from 10)
;
; Hint: DEC sets the Z flag when result is 0
; Hint: JNZ (Jump if Not Zero) jumps when Z=0
; Hint: This creates a countdown loop

section .code

loadi r0, 10 ; Load 10 into R0
loadi r0, 10

loop:
dec r0 ; Decrement R0
jnz loop ; Jump to loop if R0 is not zero (JNZ)
halt ; Halt (R0 should be 0)
dec r0
jnz loop
halt
17 changes: 7 additions & 10 deletions asm/tutorial/11_count_up.asm
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
; Exercise 11: Count Up
; Level: 2 - Control Flow
;
; Goal: Count from 0 up to 10 using INC and CMP
; Stop when R0 equals 10
;
; Instructions to use: LOADI, INC, CMP, JZ, JMP, HALT
; Expected result: R0 = 10
;
; Hint: CMP R0, R1 compares R0 with R1, sets Z flag if equal
; Hint: JZ (Jump if Zero) jumps when Z=1 (values are equal)
; Hint: Use JMP to continue loop if not equal

section .code

loadi r0, 0 ; Load 0 into R0 (counter)
loadi r1, 10 ; Load 10 into R1 (target)
loadi r0, 0
loadi r1, 10
loop:
cmp r0, r1 ; Compare R0 with R1
jz done ; If equal (Z=1), jump to done
inc r0 ; Increment R0
jmp loop ; Jump back to loop
cmp r0, r1
jz done
inc r0
jmp loop
done:
halt ; Halt (R0 should be 10)
halt
17 changes: 7 additions & 10 deletions asm/tutorial/12_compare_numbers.asm
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
; Exercise 12: Compare Numbers
; Level: 2 - Control Flow
;
; Goal: Load two numbers (5 and 8) and store the maximum in R0
; Use CMP and conditional jumps
;
; Instructions to use: LOADI, CMP, JC, JMP, MOV, HALT
; Expected result: R0 = 8 (the maximum)
;
; Hint: CMP R0, R1 computes R0 - R1 and sets flags
; Hint: If R0 < R1, the C flag is set (borrow occurred)
; Hint: JC (Jump if Carry) can be used for "less than" comparisons

section .code

loadi r0, 5 ; Load 5 into R0
loadi r1, 8 ; Load 8 into R1
cmp r0, r1 ; Compare R0 with R1
jc r1_larger ; If R0 < R1 (C=1), jump to r1_larger
jmp done ; Jump to done (R0 >= R1, R0 is max)
loadi r0, 5
loadi r1, 8
cmp r0, r1
jc r1_larger
jmp done
r1_larger:
mov r0, r1 ; Move R1 to R0 (R1 is the max)
mov r0, r1
done:
halt ; Halt (R0 should be 8)
halt
19 changes: 8 additions & 11 deletions asm/tutorial/13_if_else.asm
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
; Exercise 13: If-Else Logic
; Level: 2 - Control Flow
;
; Goal: Implement if-else logic
; If R0 >= 10, set R2 to 100
; Else, set R2 to 50
;
; Instructions to use: LOADI, CMP, JC, JMP, HALT
; Expected result: R2 = 50 (since R0 = 7 < 10)
;
; Hint: Structure is: compare, conditional jump to else, then-block, jump over else, else-block
; Hint: CMP followed by JC tests for "less than"

section .code

loadi r0, 7 ; Load 7 into R0
loadi r1, 10 ; Load 10 into R1 (threshold)
cmp r0, r1 ; Compare R0 with R1
jc else_block ; If R0 < R1 (C=1), jump to else_block
loadi r0, 7
loadi r1, 10
cmp r0, r1
jc else_block
then_block:
loadi r2, 100 ; Load 100 into R2
jmp done ; Jump to done
loadi r2, 100
jmp done
else_block:
loadi r2, 50 ; Load 50 into R2
loadi r2, 50
done:
halt ; Halt (R2 should be 50)
halt
19 changes: 8 additions & 11 deletions asm/tutorial/14_for_loop.asm
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
; Exercise 14: For Loop
; Level: 2 - Control Flow
;
; Goal: Sum numbers from 1 to 5 using a for-loop pattern
; R0 = accumulator (sum), R1 = counter, R2 = limit
;
; Instructions to use: LOADI, ADD, INC, CMP, JNZ, HALT
; Expected result: R0 = 15 (1+2+3+4+5)
;
; Hint: For-loop pattern: initialize counter, loop body, increment, test, repeat
; Hint: Sum should include: 1+2+3+4+5 = 15

section .code

loadi r0, 0 ; Load 0 into R0 (sum accumulator)
loadi r1, 1 ; Load 1 into R1 (counter, start at 1)
loadi r2 6 ; Load 6 into R2 (limit, stop when counter reaches 6)
loadi r0, 0
loadi r1, 1
loadi r2 6
loop:
add r0, r1 ; Add R1 to R0 (accumulate sum)
inc r1 ; Increment R1 (next number)
cmp r1, r2 ; Compare R1 with R2
jnz loop ; If not equal, jump back to loop
halt ; Halt (R0 should be 15)
add r0, r1
inc r1
cmp r1, r2
jnz loop
halt
Loading
Loading