Skip to content

Commit 9d70623

Browse files
committed
shellblocks: primitives: print: Use noreorder and cleaner code
1 parent e460cb8 commit 9d70623

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

shellblocks/primitives/print.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ShellcodePrimitivePrint(ShellcodePrimitive):
55
def __init__(self, nickname: str, print_function: int, print_string: str):
66
super().__init__(
77
nickname,
8-
["print.S", "utils.h"],
8+
["print.S", "utils_asm.h"],
99
"print.S",
1010
"print.h"
1111
)

shellblocks/src/print.S

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#include "print.h"
2+
#include "utils_asm.h"
3+
4+
.set noreorder
25

36
.global start
47
start:
58
// Save $ra
69
addiu $sp, -4
710
sw $ra, 0($sp)
811

9-
// Get $pc using bal
10-
bal code
11-
nop
12-
code:
13-
// bal somehow compiles to "bal + nop" so 2 opcodes
14-
addiu $a0, $ra, (print_string - code + 4)
15-
nop
12+
// Calculate address of `print_string`
13+
// relative to current $pc
14+
GET_PC($v0)
15+
GET_ADDRESS($a0, print_string, $v0)
1616

1717
lui $v0, %hi(PRINT_FUNCTION_ADDRESS)
1818
addiu $v0, %lo(PRINT_FUNCTION_ADDRESS)
@@ -27,10 +27,12 @@ code:
2727
// Jump over the printed string, to ensure we can run
2828
// another primitive after this one.
2929
b end_of_code
30+
nop
3031

3132
print_string:
3233
.asciiz PRINT_STRING
3334
.align 2
3435

3536
end_of_code:
3637
nop
38+

0 commit comments

Comments
 (0)