CS代考 Tutorial: loops, memory and procedures

Tutorial: loops, memory and procedures

Objectives

Copyright By PowCoder代写 加微信 powcoder

• Writing a MIPS program walkthrough.
• Revise memory access, loop, syscalls and procedures in MIPS
• Explore how basic logical operations can be used for security.

XOR encryption
• XOR truth table
• return 1 if the input bits are different, otherwise return 0.
• Properties: A^A = 0, (TEXT^KEY)^KEY = TEXT
• XOR is a building blocks in many encryption algorithms (e.g. AES).

int main(int argc, char **argv) {
char plaintext[13], encrypt[13], key[5] = “abcd”; int key_len = 4, i;
printf(“> “);
fgets(plaintext, 13, stdin);
for (i = 0 ; i < 12 ; i++) printf("0x%02x ", plaintext[i]); printf("\n"); for (i=0; i < 12; i++) encrypt[i] = plaintext[i] ^ key[i%4]; for (i = 0 ; i < 12 ; i++) printf("0x%02x ", encrypt[i]); printf("\n"); for (i=0; i < 12; i++) encrypt[i] = encrypt[i] ^ key[i%4]; for (i = 0 ; i < 12 ; i++) printf("0x%02x ", encrypt[i]); printf("\n"); return 0; } ➜ ~ gcc -o encrypt test.c ➜ ~ ./encrypt > hello world
Input 0x68 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64 0x0a Encrypt 0x09 0x07 0x0f 0x08 0x0e 0x42 0x14 0x0b 0x13 0x0e 0x07 0x6e Decrypt 0x68 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64 0x0a

memory layout

MIPS data and text
• “.text” directive
• Instructions, copied to the Text memory segment.
• “.data” directive
• Data put in the Data segment starting at address 0x1001 0000

MIPS program
plaintext : .byte 0:12 encrypt : .byte 0:12 key : .ascii “abcd”
newline : prompt : space :
.asciiz “\n” .asciiz “> ”
.asciiz ” ”

DATA in memory

• Read a string from the command-line (assume 12 characters) and store it in memory.
• Loop over the input string characters:
– Read a char from the input string.
– Read a char from the key.
– XOR the two bytes and store the resulting byte in memory.
• Printtheencryptedstringinhex.

READ INPUT string
j main main:
# load memory address of plain text la $a0, plaintext
addi $a1, $zero, 12
syscall # use a syscall to read the string

j main main:
# load address to save plain text la $a0, plaintext
addi $a1, $zero, 12
syscall # use a syscall to read the string
# initialise variables
li $t0, 0 #loop over the input string characters loop:
addi $t0, $t0, 1 bne $t0, 12, loop

read input string bytes
j main main:
# load address to save plain text la $a0, plaintext
addi $a1, $zero, 12
li $v0, 8a
syscall # use a syscall to read the string
# initialize variables
li $t0, 0 #loop over the input string characters
la $t1, plaintext
add $t4, $t1, $t0
lb $t5, ($t4) # load a byte from the input string
addi $t0, $t0, 1 bne $t0, 12, loop
Hint: we process string byte by byte

Load key bytes
j main main:
# load address to save plain text la $a0, plaintext
addi $a1, $zero, 12
li $v0, 8a
syscall # use a syscall to read the string
# initialize variables
li $t0, 0 #loop over the input string characters la $t1, plaintext
la $t3, key
add $t4, $t1, $t0
lb $t5, ($t4) # load a byte from the input string
rem $t4, $t0, 4
add $t4, $t3, $t4
lb $t6, ($t4) # load a key byte
addi $t0, $t0, 1 bne $t0, 12, loop
Hint: we use a modulo operation to wrap around the key string

Encrypt and store bytes
# initialize variables
li $t0, 0 #loop over the input string characters la $t1, plaintext
la $t2, encrypt
la $t3, key
add $t4, $t1, $t0 lb $t5, ($t4)
rem $t4, $t0, 4 add $t4, $t3, $t4 lb $t6, ($t4)
# load a byte from the input string
# load a key byte xor $t6, $t6, $t5 # encrypt
add $t4, $t2, $t0
sb $t6, ($t4) # store encrypted byte
addi $t0, $t0, 1 bne $t0, 12, loop

Print encryption result
j main main:
# load address to save plain text la $a0, plaintext
addi $t0, $t0, 1
bne $t0, 12, loop
# reset index
li $t0, 0 print:
add $t4, $t2, $t0 # convert 4 bytes to lw $a0, ($t4) # an int and
li $v0, 34 # print the hex
add $t0, $t0, 4 bne $t0, 12, print

Organise your code using procedures
• The same XORing code can be used to encrypt and decrypt a message.
• Create a procedure to XOR a message.
– $a0: plaintext address
– $a1: encrypted text address – assume 4 bytes
– $a2: length of encryption message
• Create a procedure to print a string in hex.
– $a0: byte array address
– $a1: byte array length

encryption procedure
encrypt_proc:
# initialize temp variables li $t0, 0
la $t3, key
add $t4, $a0, $t0
lb $t5, ($t4) # load encrypted byte
rem $t4, $t0, 4
add $t4, $t3, $t4
lb $t6, ($t4) # load key byte
xor $t6, $t6, $t5 # encrypt
add $t4, $a1, $t0
sb $t6, ($t4) # store encrypted byte
addi $t0, $t0, 1
bne $t0, $a2, loop # repeat your loop jr $ra # return procedure
# call encryption method la $a0, plaintext
la $a1, encrypt
li $a2, 12
jal encrypt_proc
# call decryption method la $a0, encrypt
la $a1, encrypt
li $a2, 12
jal encrypt_proc

Print procedure
print_string: li $t0, 0
add $t1, $a0, $zero print:
add $t2, $t1, $t0 lw $a0, ($t2)
li $v0, 34 syscall
# print a space la $a0, space li $v0, 4 syscall
addi $t0, $t0, 4 bne $t0, $a1, print
# print a newline la $a0, newline li $v0, 4
la $a0, plaintext li $a1, 12
jal print_string
la $a0, encrypt li $a1, 12
jal print_string

Preserving registers
• MIPS convention
• s registers must be restored after procedure call
• If usage of these registers is avoided no spilling of registers on the stack is required.
P&H fig. 2.15
DONT FORGET

SPILL $S registers
print_string:
addi $sp, $sp, -8 sw $s1, 4($sp) sw $s0, 0($sp)
add $s1, $a0, $zero print:
add $t2, $s1, $s0 lw $a0, ($t2)
li $v0, 34
# print a newline la $a0, newline
lw $s1, 4($sp) lw $s0, 0($sp) addi $sp, $sp, 8

Memory access Syscalls Procedures

Final encryption program
plaintext : .byte 0:12 encrypt : .byte 0:12 key : .ascii “abcd” newline : .asciiz “\n” prompt : .asciiz “> ” space : .asciiz ” ”
encrypt_proc:
# iniialize our temp variables li $t0, 0
la $t3, key
add $t4, $a0, $t0 lb $t5,($t4)
rem $t4, $t0, 4 add $t4, $t3, $t4 lb $t6, ($t4)
xor $t6, $t6, $t5
add $t4, $a1, $t0 sb $t6, ($t4)
addi $t0, $t0, 1 bne $t0, $a2, loop jr $ra
add $t1, $a0, $zero print:
add $t2, $t1, $t0 lw $a0, ($t2)
li $v0, 34
# print a space la $a0, space li $v0, 4 syscall
jal print_string
print_string:

Final encryption program
addi $t0, $t0, 4
# print a prompt la $a0, prompt
bne $t0, $a1, print
# print a newline la $a0, newline
# load address to save plain text la $a0, plaintext
addi $a1, $zero, 12
# print a newline la $a0, newline
la $a0, plaintext li $a1, 12
jal print_string
# call encryption method la $a0, plaintext
la $a1, encrypt
li $a2, 12
jal encrypt_proc
la $a0, encrypt li $a1, 12
jal print_string
# call encryption method la $a0, encrypt
la $a1, encrypt
li $a2, 12
jal encrypt_proc
la $a0, encrypt li $a1, 12

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com