| 1004 | // t0=x5, t1=x6, sp=x2, a0=x10, a7=x17 |
| 1005 | // Store 99 to stack scratch space, load it back into a0, exit with it. |
| 1006 | let (_, outcome) = assemble_and_run(vec![ |
| 1007 | ri(RealInstruction::Addi(Addi::new(5, 0, 99))), // t0 = 99 |
| 1008 | ri(RealInstruction::Addi(Addi::new(6, 2, -8))), // t1 = sp - 8 (scratch address) |
| 1009 | ri(RealInstruction::Sd(Sd::new(6, 5, 0))), // sd t0, 0(t1) - store 99 |
| 1010 | ri(RealInstruction::Ld(Ld::new(10, 6, 0))), // ld a0, 0(t1) - load 99 |
| 1011 | ri(RealInstruction::Addi(Addi::new(17, 0, 93))), // a7 = 93 |
| 1012 | ri(RealInstruction::Ecall(Ecall::new())), |
| 1013 | ]); |
| 1014 | assert!( |
| 1015 | matches!(outcome, RunOutcome::Halted(99)), |
| 1016 | "expected Halted(99), got {outcome:?}" |
| 1017 | ); |
| 1018 | } |
| 1019 | |
| 1020 | #[test] |
| 1021 | fn test_branch_loop() { |
| 1022 | // Count down from 5 to 0, then exit with 0. The bne offset is in bytes, so |
| 1023 | // -4 branches back one instruction. |
| 1024 | let (_, outcome) = assemble_and_run(vec![ |
| 1025 | ri(RealInstruction::Addi(Addi::new(5, 0, 5))), // t0 = 5 |
| 1026 | ri(RealInstruction::Addi(Addi::new(5, 5, -1))), // t0 -= 1 (loop body) |
| 1027 | ri(RealInstruction::Bne(Bne::new(5, 0, -4))), // if t0 != 0, branch to loop body |
| 1028 | ri(RealInstruction::Add(Add::new(10, 5, 0))), // a0 = t0 (= 0) |
| 1029 | ri(RealInstruction::Addi(Addi::new(17, 0, 93))), // a7 = 93 |
| 1030 | ri(RealInstruction::Ecall(Ecall::new())), |
| 1031 | ]); |
| 1032 | assert!( |
| 1033 | matches!(outcome, RunOutcome::Halted(0)), |
| 1034 | "expected Halted(0), got {outcome:?}" |
| 1035 | ); |
| 1036 | } |