├── Makefile └── cesar.s /Makefile: -------------------------------------------------------------------------------- 1 | clean: 2 | rm cesar 3 | rm cesar.o 4 | cesar.o: 5 | as -o cesar.o cesar.s 6 | cesar: cesar.o 7 | ld -o cesar --no-relax cesar.o 8 | strace: cesar 9 | strace -tT ./cesar 10 | dump: cesar 11 | objdump -d cesar 12 | labels: cesar 13 | objdump -x cesar 14 | -------------------------------------------------------------------------------- /cesar.s: -------------------------------------------------------------------------------- 1 | # Cesar Cypher in RISCV-64 Assembly 2 | 3 | .section .data 4 | msg_message: .string "Message: " 5 | msg_key: .string "Key: " 6 | msg_cypher: .string "Cypher: " 7 | msg_incorrect: .string "Incorrect key\n" 8 | original: .space 100 9 | key_buffer: .space 100 10 | cypher: .space 100 11 | .section .text 12 | .global _start 13 | _start: 14 | # Print Message 15 | li a7, 64 # Linux Write syscall is 64 16 | li a0, 1 17 | la a1, msg_message 18 | li a2, 9 19 | ecall 20 | 21 | # Read original message 22 | li a7, 63 # Linux Read syscall is 63 23 | li a0, 0 24 | la a1, original 25 | li a2, 99 26 | ecall 27 | 28 | # Print Key message 29 | li a7, 64 30 | li a0, 1 31 | la a1, msg_key 32 | la a2, 9 33 | ecall 34 | 35 | # Read (string) input of key 36 | li a7, 63 37 | li a0, 0 38 | la a1, key_buffer 39 | la a2, 99 40 | ecall 41 | 42 | la a0, key_buffer 43 | jal parse_key 44 | move a1, a0 45 | 46 | la a0, original 47 | la a2, cypher 48 | jal ra, cesar 49 | move s0, a0 50 | 51 | # Print cypher message 52 | li a7, 64 53 | li a0, 1 54 | la a1, msg_cypher 55 | li a2, 9 56 | ecall 57 | 58 | beqz s0, cypher_ok 59 | # Print incorrect message 60 | li a7, 64 61 | li a0, 1 62 | la a1, msg_incorrect 63 | li a2, 14 64 | ecall 65 | j exit 66 | 67 | cypher_ok: 68 | # Print cyphered message 69 | li a7, 64 70 | li a0, 1 71 | la a1, cypher 72 | li a2, 99 73 | ecall 74 | 75 | exit: li a7, 93 # Linux Exit syscall is 93 76 | li a0, 0 # Return code 77 | ecall 78 | 79 | # Parse string key to integer 80 | # a0 - Location of key buffer 81 | # a0 - Return of integer if no error 82 | parse_key: 83 | li t0, 0 84 | li t1, 10 85 | li t3, '\n' 86 | li t4, '-' 87 | lbu t2, 0(a0) 88 | beq t2, t4, parse_key_loop_negative 89 | parse_key_loop: 90 | lbu t2, 0(a0) 91 | beq t2, t3, exit_parse_key 92 | mul t0, t0, t1 93 | addi t2, t2, -48 94 | add t0, t0, t2 95 | addi a0, a0, 1 96 | j parse_key_loop 97 | parse_key_loop_negative: 98 | lbu t2, 1(a0) 99 | beq t2, t3, exit_parse_key 100 | mul t0, t0, t1 101 | addi t2, t2, -48 102 | sub t0, t0, t2 103 | addi a0, a0, 1 104 | j parse_key_loop_negative 105 | exit_parse_key: 106 | move a0, t0 107 | ret 108 | 109 | # Cesar 110 | # a0 - Location of original string 111 | # a1 - Key 112 | # a2 - Location of cypher string 113 | cesar: 114 | addi sp, sp, -32 115 | sd s0, 24(sp) 116 | sd ra, 16(sp) 117 | sd s2, 8(sp) 118 | sd s3, 0(sp) 119 | move s0, a0 120 | move s2, a2 121 | li s3, '\n' 122 | cesar_loop: 123 | lbu a0, 0(s0) 124 | beq a0, s3, exit_cesar_loop 125 | jal cesar_char 126 | sb a0, 0(s2) 127 | addi s0, s0, 1 128 | addi s2, s2, 1 129 | j cesar_loop 130 | exit_cesar_loop: 131 | sb a0, 0(s2) 132 | sb zero, 1(s2) 133 | li a0, 0 134 | ld s0, 24(sp) 135 | ld ra, 16(sp) 136 | ld s2, 8(sp) 137 | ld s3, 0(sp) 138 | addi sp, sp, 32 139 | jalr zero, ra 140 | 141 | # Cesar char 142 | # Inputs: 143 | # a0 - Character 144 | # a1 - Key 145 | # Outputs: 146 | # a0 - Cypher char 147 | cesar_char: 148 | add a0, a0, a1 149 | li t0, 32 150 | li t1, 126 151 | blt a0, t0, cesar_char_under_32 152 | bgt a0, t1, cesar_char_over_126 153 | ret 154 | cesar_char_under_32: 155 | sub t0, t0, a0 156 | sub a0, t1, t0 157 | addi a0, a0, 1 158 | ret 159 | cesar_char_over_126: 160 | sub t1, a0, t1 161 | add a0, t0, t1 162 | addi a0, a0, -1 163 | ret 164 | --------------------------------------------------------------------------------