├── README.md ├── lab-exercises ├── 1 │ ├── q1_1 │ │ ├── 1_1.s │ │ └── README.md │ ├── q1_2 │ │ ├── 1_2_a.s │ │ ├── 1_2_b.s │ │ ├── 1_2_c.s │ │ ├── 1_2_d.s │ │ ├── 1_2_e.s │ │ └── README.md │ ├── q1_3 │ │ ├── 1_3_a.s │ │ └── README.md │ ├── q1_4 │ │ ├── 1_4.s │ │ └── README.md │ ├── q1_5 │ │ ├── 1_5.s │ │ └── README.md │ └── q1_6 │ │ ├── 1_6.s │ │ └── README.md ├── 2 │ ├── q2_1 │ │ ├── 2_1.s │ │ └── README.md │ ├── q2_2 │ │ ├── 2_2.s │ │ ├── 2_2_mask.s │ │ └── README.md │ ├── q2_3 │ │ ├── 2_3.s │ │ └── README.md │ ├── q2_5 │ │ ├── 2_5.s │ │ └── README.md │ ├── q2_6 │ │ ├── 2_6.s │ │ └── README.md │ ├── q2_7 │ │ ├── 2_7.s │ │ └── README.md │ └── q2_8 │ │ ├── 2_8.s │ │ └── README.md ├── 3 │ ├── q3_1 │ │ ├── 3_1_a.s │ │ ├── 3_1_b.s │ │ └── README.md │ ├── q3_2 │ │ ├── 3_2.s │ │ └── README.md │ ├── q3_3 │ │ ├── 3_3_a.s │ │ ├── 3_3_b.s │ │ └── README.md │ └── q3_4 │ │ └── 3_4.s ├── 4 │ ├── q4_1 │ │ ├── 4_1_a_d.s │ │ ├── 4_1_a_m.s │ │ └── README.md │ ├── q4_2 │ │ ├── 4_2.s │ │ └── README.md │ └── q4_3 │ │ ├── 4_3.s │ │ └── README.md ├── 5 │ ├── README.md │ ├── q5_1 │ │ ├── 5_1.s │ │ └── README.md │ ├── q5_2 │ │ ├── 5_2.s │ │ └── README.md │ └── q5_3 │ │ ├── 5_3_a.s │ │ ├── 5_3_b.s │ │ └── README.md └── README.md ├── previous-exams ├── README.md ├── exams │ ├── CommonCharsin2Strings.s │ ├── MultOFVectorsFloat.s │ ├── README.md │ └── SecondDegreeEquationFloat.s ├── practice │ ├── Binary.s │ ├── FloatToSumOf1n0.s │ ├── PowerofaNumber.s │ ├── SameLetterStrings.s │ ├── ScientificNotation.s │ ├── StringFourNumber.s │ ├── StringInterleaving.s │ ├── StringPyramid.s │ ├── SumVectrorsFloat.s │ ├── sdi2100046-achlab.s │ ├── sim1.s │ ├── sim2.s │ └── sim3.s └── simulation-exam-2022 │ ├── README.md │ ├── ekfwnisi.jpg │ ├── exam.s │ ├── exam2.s │ └── exam3.s └── useful-code ├── all_display.s ├── basic_procedures.s ├── branches.s ├── other procedures ├── bit_isolation.s ├── char_isolation.s ├── count_digits.s ├── min_max.s └── num_power.s ├── stack_and_functios.s ├── system_call_codes.md └── template.s /README.md: -------------------------------------------------------------------------------- 1 | # ASSEMPLY MIPS examples and codes 2 | 3 | # Context 4 | During the lab of Architecture of Computer we wrote a lot of assembly code. 5 | 6 | I thought about saving and organising it in a repository, for personal use. 7 | 8 | After the classes ended I realised the plethora of code that I had in the repository. 9 | 10 | So me and Efi(@effieep) decided to organize the code and post it, in order to: 11 | 12 | * Help any student of DIT,University Of Athens with their lab. 13 | * Provide some ready to use code for someone learning assembly. 14 | 15 | # Contents 16 | ## lab-exercises 17 | The code for all the exercises we completed in the lab. 18 | 19 | ## exams 20 | All the exam material of our and previous years solved. 21 | 22 | ## useful-code 23 | Some useful TOOLS and code as template and ready to use functions. These files, practically used as a library, provided a tool-box that allowed as to focus on higher-level problems, rather than writing evey lin eby hand 24 | 25 | # Contribution - Efi Papafili 26 | The contribution of Efi in the repository and the lab has been really game-changing! 27 | See [Efi's github](https://github.com/effieep) 28 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_1/1_1.s: -------------------------------------------------------------------------------- 1 | ################################################### 2 | # # 3 | # lab1_1.s # 4 | # Pseudoinstruction examples - System calls # 5 | # t0 - holds each byte from string in turn # 6 | # t1 - contains count of characters # 7 | # t2 - points to the string # 8 | # # 9 | ################################################### 10 | ################################################### 11 | # # 12 | # text segment # 13 | # # 14 | ################################################### 15 | .text 16 | .globl __start 17 | __start: 18 | la $t2,str # t2 points to the string 19 | li $t1,0 # t1 holds the count 20 | nextCh: lb $t0,($t2) # get a byte from string 21 | beqz $t0,strEnd # zero means end of string 22 | add $t1,$t1,1 # increment count 23 | add $t2,1 # move pointer one character 24 | j nextCh # go round the loop again 25 | strEnd: la $a0,ans # system call to print 26 | li $v0,4 # out a message 27 | syscall 28 | move $a0,$t1 # system call to print 29 | li $v0,1 # out the length worked out 30 | syscall 31 | la $a0,endl # system call to print 32 | li $v0,4 # out a newline 33 | syscall 34 | 35 | li $v0,10 36 | syscall # au revoir... 37 | ################################################# 38 | # # 39 | # data segment # 40 | # # 41 | ################################################# 42 | .data 43 | str: .asciiz "hello world" 44 | ans: .asciiz "Length is " 45 | endl: .asciiz "\n" 46 | ################################################# 47 | # # 48 | # End of File # 49 | # # 50 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/1/q1_1/README.md: -------------------------------------------------------------------------------- 1 | # question 1.1 2 | 3 | Copied code from notes and showcased some basics of the way the memory is used in MIPS processor. -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/1_2_a.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | byte1: .byte 0x04, 0x03, 0x02, 0x01 10 | byte2: .byte 0x84, 0x83, 0x82, 0x81 11 | word1: .word 0x12345678 12 | word2: .word 0x87654321 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | lb $t0, byte1 + 3 25 | lb $t1, byte1 + 2 26 | lb $t2, byte1 + 1 27 | lb $t3, byte1 28 | sb $t0, 0x10010000 29 | sb $t1, 0x10010001 30 | sb $t2, 0x10010002 31 | sb $t3, 0x10010003 32 | 33 | lb $t0, byte2 + 3 34 | lb $t1, byte2 + 2 35 | lb $t2, byte2 + 1 36 | lb $t3, byte2 37 | sb $t0, 0x10010004 38 | sb $t1, 0x10010005 39 | sb $t2, 0x10010006 40 | sb $t3, 0x10010007 41 | 42 | lw $t0, word1 43 | sw $t0, 0x10010008 44 | 45 | lw $t0, word2 46 | sw $t0, 0x1001000c 47 | j Exit 48 | 49 | Exit: li $v0, 10 50 | syscall #au revoir... 51 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/1_2_b.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | byte1: .byte 0x04, 0x03, 0x02, 0x01 10 | byte2: .byte 0x84, 0x83, 0x82, 0x81 11 | word1: .word 0x12345678 12 | word2: .word 0x87654321 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | lb $t0, byte1 + 3 ################################################ 25 | lb $t1, byte1 + 2 26 | lb $t2, byte1 + 1 27 | lb $t3, byte1 28 | sb $t0, 0x10010000 29 | sb $t1, 0x10010001 30 | sb $t2, 0x10010002 31 | sb $t3, 0x10010003 32 | 33 | lb $t0, byte2 + 3 34 | lb $t1, byte2 + 2 #QUESTION 1 35 | lb $t2, byte2 + 1 36 | lb $t3, byte2 37 | sb $t0, 0x10010004 38 | sb $t1, 0x10010005 39 | sb $t2, 0x10010006 40 | sb $t3, 0x10010007 41 | 42 | lw $t0, word1 43 | sw $t0, 0x10010008 44 | 45 | lw $t0, word2 46 | sw $t0, 0x1001000c ##################################################### 47 | 48 | lbu $t0, 0x10010000 49 | lhu $t1, 0x10010000 #QUESTION 2 50 | lw $t2, 0x10010000 51 | 52 | j Exit 53 | Exit: li $v0, 10 54 | syscall #au revoir... 55 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/1_2_c.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | byte1: .byte 0x04, 0x03, 0x02, 0x01 10 | byte2: .byte 0x84, 0x83, 0x82, 0x81 11 | word1: .word 0x12345678 12 | word2: .word 0x87654321 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg# 23 | __start: 24 | lb $t0, byte1 + 3 ################################################ 25 | lb $t1, byte1 + 2 26 | lb $t2, byte1 + 1 27 | lb $t3, byte1 28 | sb $t0, 0x10010000 29 | sb $t1, 0x10010001 30 | sb $t2, 0x10010002 31 | sb $t3, 0x10010003 32 | 33 | lb $t0, byte2 + 3 34 | lb $t1, byte2 + 2 #QUESTION 1 35 | lb $t2, byte2 + 1 36 | lb $t3, byte2 37 | sb $t0, 0x10010004 38 | sb $t1, 0x10010005 39 | sb $t2, 0x10010006 40 | sb $t3, 0x10010007 41 | 42 | lw $t0, word1 43 | sw $t0, 0x10010008 44 | 45 | lw $t0, word2 46 | sw $t0, 0x1001000c ##################################################### 47 | 48 | lbu $t0, 0x10010000 #QUESTION 2 49 | lhu $t1, 0x10010000 50 | lw $t2, 0x10010000 ##################################################### 51 | 52 | lbu $t3, 0x10010004 #QUESTION 3 53 | lhu $t4, 0x10010004 54 | lw $t5, 0x10010004###################################################### 55 | 56 | j Exit 57 | Exit: li $v0, 10 58 | syscall #au revoir... 59 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/1_2_d.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | byte1: .byte 0x04, 0x03, 0x02, 0x01 10 | byte2: .byte 0x84, 0x83, 0x82, 0x81 11 | word1: .word 0x12345678 12 | word2: .word 0x87654321 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg# 23 | __start: 24 | lb $t0, byte1 + 3 #################################################### 25 | lb $t1, byte1 + 2 26 | lb $t2, byte1 + 1 27 | lb $t3, byte1 28 | sb $t0, 0x10010000 29 | sb $t1, 0x10010001 30 | sb $t2, 0x10010002 31 | sb $t3, 0x10010003 32 | 33 | lb $t0, byte2 + 3 34 | lb $t1, byte2 + 2 #QUESTION 1 35 | lb $t2, byte2 + 1 36 | lb $t3, byte2 37 | sb $t0, 0x10010004 38 | sb $t1, 0x10010005 39 | sb $t2, 0x10010006 40 | sb $t3, 0x10010007 41 | 42 | lw $t0, word1 43 | sw $t0, 0x10010008 44 | 45 | lw $t0, word2 46 | sw $t0, 0x1001000c ##################################################### 47 | 48 | lbu $t0, 0x10010000 #QUESTION 2 49 | lhu $t1, 0x10010000 50 | lw $t2, 0x10010000 ##################################################### 51 | 52 | lbu $t3, 0x10010004 #QUESTION 3 53 | lhu $t4, 0x10010004 54 | lw $t5, 0x10010004###################################################### 55 | 56 | lb $t0, 0x10010000 57 | lh $t1, 0x10010000 #QUESTION 4 58 | lw $t2, 0x10010000 59 | lb $t3, 0x10010004 60 | lh $t4, 0x10010004 61 | lw $t5, 0x10010004###################################################### 62 | 63 | j Exit 64 | Exit: li $v0, 10 65 | syscall #au revoir... 66 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/1_2_e.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | byte1: .byte 0x04, 0x03, 0x02, 0x01 10 | byte2: .byte 0x84, 0x83, 0x82, 0x81 11 | word1: .word 0x12345678 12 | word2: .word 0x87654321 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | lb $t0, byte1 + 3 ################################################ 25 | lb $t1, byte1 + 2 26 | lb $t2, byte1 + 1 27 | lb $t3, byte1 28 | sb $t0, 0x10010000 29 | sb $t1, 0x10010001 30 | sb $t2, 0x10010002 31 | sb $t3, 0x10010003 32 | 33 | lb $t0, byte2 + 3 34 | lb $t1, byte2 + 2 #QUESTION 1 35 | lb $t2, byte2 + 1 36 | lb $t3, byte2 37 | sb $t0, 0x10010004 38 | sb $t1, 0x10010005 39 | sb $t2, 0x10010006 40 | sb $t3, 0x10010007 41 | 42 | lw $t0, word1 43 | sw $t0, 0x10010008 44 | 45 | lw $t0, word2 46 | sw $t0, 0x1001000c ##################################################### 47 | 48 | lbu $t0, 0x10010000 #QUESTION 2 49 | lhu $t1, 0x10010000 50 | lw $t2, 0x10010000 ##################################################### 51 | 52 | lbu $t3, 0x10010004 #QUESTION 3 53 | lhu $t4, 0x10010004 54 | lw $t5, 0x10010004 ##################################################### 55 | 56 | lb $t0, 0x10010000 57 | lh $t1, 0x10010000 #QUESTION 4 58 | lw $t2, 0x10010000 59 | lb $t3, 0x10010004 60 | lh $t4, 0x10010004 61 | lw $t5, 0x10010004 ##################################################### 62 | 63 | lb $t6, 0x10010004 #QUESTION 5 64 | sb $t6, 0x10010010 65 | 66 | 67 | j Exit 68 | Exit: li $v0, 10 69 | syscall #au revoir... 70 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_2/README.md: -------------------------------------------------------------------------------- 1 | # question 1.2 2 | practice store, load, breakpoints. -------------------------------------------------------------------------------- /lab-exercises/1/q1_3/1_3_a.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | .align 0 9 | bytes: .byte 0x01,0x02,0x03,0x04,0x05,0x81,0x082,0x83,0x84 10 | half: .half 0x6677 11 | words: .word 0x12345678 , 0x87654321 12 | ################################################# 13 | # # 14 | # text segment # 15 | # # 16 | ################################################# 17 | .text 18 | .globl __start 19 | __start: 20 | #-------------start of main program-------------# 21 | #la $s0, bytes 22 | #lw $t0, 12($s0) 23 | 24 | #la $s0, half 25 | #lh $t1, 0($s0) 26 | 27 | la $s0, bytes 28 | ulh $t1, 9($s0) 29 | ulw $t2, 13($s0) 30 | exit: li $v0, 10 31 | syscall #au revoir... 32 | #-------------end of main program--------------# 33 | 34 | #-------------start of procedures--------------# 35 | 36 | 37 | #-------------end of procedures---------------# 38 | 39 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_3/README.md: -------------------------------------------------------------------------------- 1 | # question 1.3 2 | Aligning -------------------------------------------------------------------------------- /lab-exercises/1/q1_4/1_4.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str: .space 21 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | 21 | 22 | li $v0, 8 # code to read a string 23 | li $a1, 21 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 24 | la $a0, str # ??? --> label with string 25 | syscall 26 | 27 | li $v0, 4 28 | syscall 29 | 30 | la $a0,endl # system call to print 31 | li $v0, 4 # out a newline 32 | syscall 33 | 34 | la $a0, str 35 | sb $zero, str + 5 36 | syscall 37 | 38 | 39 | li $v0, 10 40 | syscall #au revoir... 41 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_4/README.md: -------------------------------------------------------------------------------- 1 | # question 1.4 2 | 3 | reading and printing **strings** with system calls -------------------------------------------------------------------------------- /lab-exercises/1/q1_5/1_5.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str: .space 21 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | read_int: li $v0, 5 # $v0 <--- integer 21 | syscall 22 | addi $t0, $v0, 0 23 | 24 | print_int_from_reg: move $a0, $t0 # ??? --> register with integer 25 | li $v0, 1 26 | syscall 27 | 28 | Exit: li $v0, 10 29 | syscall #au revoir... 30 | 31 | 32 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_5/README.md: -------------------------------------------------------------------------------- 1 | # question 1.5 2 | 3 | reading and printing **integers** with systemcalls -------------------------------------------------------------------------------- /lab-exercises/1/q1_6/1_6.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg1: .asciiz "Give first number to add: " 10 | msg2: .asciiz "Give second number to add: " 11 | msg3: .asciiz "Sum of the two numbers is: " 12 | msg4: .asciiz "Diff of the two numbers is: " 13 | ################################################# 14 | # # 15 | # text segment # 16 | # # 17 | ################################################# 18 | 19 | .text 20 | .globl __start 21 | #read first int and move it to a temp reg 22 | __start: 23 | li $v0, 4 #printf msg1 24 | la $a0, msg1 25 | syscall 26 | 27 | li $v0, 5 #read int 28 | syscall 29 | move $t0, $v0 30 | 31 | move $a0, $t0 #print int 32 | li $v0 , 1 33 | syscall 34 | 35 | la $a0, endl #print \n 36 | li $v0, 4 37 | syscall 38 | #################################################################### 39 | li $v0, 4 #printf msg2 40 | la $a0, msg2 41 | syscall 42 | 43 | li $v0, 5 #read int 44 | syscall 45 | move $t1, $v0 46 | 47 | move $a0, $t1 #print int 48 | li $v0 , 1 49 | syscall 50 | 51 | la $a0, endl #print \n 52 | li $v0, 4 53 | syscall 54 | #################################################################### 55 | li $v0, 4 #printf msg3 56 | la $a0, msg3 57 | syscall 58 | 59 | add $t2, $t0, $t1 # a + b = $t2 60 | 61 | move $a0, $t2 #print int 62 | li $v0 , 1 63 | syscall 64 | 65 | la $a0, endl #print \n 66 | li $v0, 4 67 | syscall 68 | ################################################################### 69 | li $v0, 4 #printf msg4 70 | la $a0, msg4 71 | syscall 72 | 73 | sub $t3, $t0, $t1 # a - b = $t3 74 | 75 | move $a0, $t3 #print int 76 | li $v0 , 1 77 | syscall 78 | 79 | la $a0, endl #print \n 80 | li $v0, 4 81 | syscall 82 | 83 | j Exit 84 | 85 | 86 | Exit: li $v0, 10 87 | syscall #au revoir... 88 | -------------------------------------------------------------------------------- /lab-exercises/1/q1_6/README.md: -------------------------------------------------------------------------------- 1 | # question 1.6 2 | printing the sum and the diff of 2 numbers -------------------------------------------------------------------------------- /lab-exercises/2/q2_1/2_1.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | bits: .word 0x01 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | .text 16 | .globl __start 17 | __start: 18 | #-------------start of main program-------------# 19 | la $s0, bits 20 | li $s1,8 21 | gray: lbu $t1,0($s0) 22 | lbu $t2,1($s0) 23 | xor $t3,$t1,$t2 24 | sb $t3, 0($s0) 25 | addi $s0,$s0,1 26 | bne $s0,$s1, gray 27 | 28 | exit: li $v0, 10 29 | syscall #au revoir... 30 | #-------------end of main program--------------# 31 | 32 | #-------------start of procedures--------------# 33 | 34 | 35 | #-------------end of procedures---------------# 36 | 37 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_1/README.md: -------------------------------------------------------------------------------- 1 | # question 2.1 2 | 3 | didnt complete in class 4 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_2/2_2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | test: .word 0x12345678 10 | 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | 22 | li $s5, 0x11223344 23 | 24 | srl $t0, $s5, 24 #isolation of byte 0 25 | 26 | sll $t1, $s5, 8 #isolation of byte 1 27 | srl $t1, $t1, 24 28 | sll $t1, $t1, 8 29 | 30 | sll $t2, $s5, 16 #isolation of byte 2 31 | srl $t2, $t2, 24 32 | sll $t2, $t2, 16 33 | 34 | sll $t3, $s5, 24 #isolation of byte 3 35 | 36 | or $s0, $t0, $t1 37 | or $s1, $t2, $t3 38 | or $s2, $s1, $s0 #result is in $s2 39 | 40 | j Exit 41 | Exit: li $v0, 10 42 | syscall #au revoir... 43 | 44 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_2/2_2_mask.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | ################################################# 10 | # # 11 | # text segment # 12 | # # 13 | ################################################# 14 | .text 15 | .globl __start 16 | __start: 17 | #-------------start of main program-------------# 18 | #s0->byte0,s1->byte 1,s2->byte2, s3->byte3 19 | #apotelesma ->s4 20 | li $a0, 0x12345678 21 | 22 | srl $s0, $a0,24 23 | 24 | sll $s3, $a0, 24 25 | 26 | #1st case with shift 27 | #2nd case with mask 28 | andi $s2,$a0, 0xff00 29 | sll $s2,$s2, 8 30 | 31 | li $t0,0xff0000 32 | and $s1,$a0,$t0 33 | sll $s1,$s1,8 34 | 35 | or $s4, $s0, $s1 36 | or $s4, $s4, $s2 37 | or $s4,$s4,$s3 38 | 39 | li $v0, 0xa # 10 40 | syscall # au revoir... 41 | 42 | 43 | Exit: li $v0, 10 44 | syscall #au revoir... 45 | #-------------end of main program--------------# 46 | 47 | #-------------start of procedures--------------# 48 | print_endl: la $a0,endl 49 | li $v0, 4 #system call to print 50 | syscall 51 | jr $ra 52 | 53 | #-------------end of procedures---------------# 54 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_2/README.md: -------------------------------------------------------------------------------- 1 | # question 2.2 2 | convert to little-endian from big-endian. -------------------------------------------------------------------------------- /lab-exercises/2/q2_3/2_3.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | number1: .word 1073741823 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | li $t0, 0xffffffff #q1-> 0x3fffffff q2->0x7fffffff q3->0xffffffff 21 | li $t1, 0x1 #q1-> 0x3fffffff q2->0x7fffffff q3->0x1 22 | 23 | addu $t4, $t0, $t1 24 | add $t3, $t0, $t1 #exception here 25 | 26 | 27 | 28 | Exit: li $v0, 10 29 | syscall #au revoir... 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_3/README.md: -------------------------------------------------------------------------------- 1 | # question 2.3 2 | tried out add and addu commands -------------------------------------------------------------------------------- /lab-exercises/2/q2_5/2_5.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | nums: .word 0xffffffff, 0xffffffff 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | 21 | lw $t0, nums + 0 22 | lw $t1, nums + 4 23 | 24 | addu $t2, $t0, $t1 25 | 26 | # Ci+1 = Si'Ai + Si'Bi + AiBi 27 | xori $t3, $t2, 1 28 | and $t4, $t3, $t0 29 | and $t5, $t3, $t1 30 | and $t6, $t0, $t1 31 | 32 | # $t3 is C32 33 | or $t3, $t4, $t5 34 | or $t3, $t3, $t6 35 | 36 | srl $t3, $t3, 31 37 | 38 | j Exit 39 | 40 | Exit: li $v0, 10 41 | syscall #au revoir... 42 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_5/README.md: -------------------------------------------------------------------------------- 1 | # question 2.5 2 | keeping in a register the carry bit in case of an overflow. -------------------------------------------------------------------------------- /lab-exercises/2/q2_6/2_6.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | nums: .word 0xffffffff, 0xffffffff 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | 21 | # A: $t1 & $t0 22 | lw $t0, nums + 0 23 | lw $t1, nums + 4 24 | # B: $t3 & $t2 25 | lw $t2, nums + 8 26 | lw $t3, nums + 12 27 | 28 | # s0 --> Slo 29 | addu $s0, $t0, $t2 30 | 31 | # t4 --> C32 32 | not $t4, $s0 33 | and $t5, $t4, $t0 34 | and $t6, $t4, $t2 35 | and $t4, $t0, $t2 36 | 37 | or $t4, $t4, $t5 38 | or $t4, $t4, $t6 39 | 40 | srl $t4, $t4, 31 41 | 42 | # s1 --> Shi 43 | addu $s1, $t4, $t1 44 | 45 | # t4 --> first upper carry 46 | # Ci+1 = Si'Ai + Si'Bi + AiBi, for Bi = 0 47 | not $t4, $s1 48 | and $t4, $t4, $t1 49 | 50 | srl $t4, $t4, 31 51 | 52 | move $t5, $s1 # save current value 53 | addu $s1, $s1, $t3 54 | 55 | # t5 --> second upper carry 56 | not $t6, $s1 57 | and $t7, $t6, $t5 58 | and $t8, $t6, $t3 59 | and $t6, $t3, $t5 60 | 61 | or $t5, $t6, $t7 62 | or $t5, $t5, $t8 63 | 64 | srl $t5, $t5, 31 65 | 66 | # t4 --> C64 67 | add $t4, $t4, $t5 68 | 69 | sw $s0, sum + 0 70 | sw $s1, sum + 4 71 | 72 | j Exit 73 | 74 | Exit: li $v0, 10 75 | syscall #au revoir... 76 | -------------------------------------------------------------------------------- /lab-exercises/2/q2_6/README.md: -------------------------------------------------------------------------------- 1 | # qustion 2.6 2 | 3 | keeps the carry of a 64 bit (32 and 32) addition in case it overflows. -------------------------------------------------------------------------------- /lab-exercises/2/q2_7/2_7.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | num1: .word 0x80000000 #p1->0xa, p2->0x2 , p3-> 0x8000000 10 | num2: .word 0x80000000 #p1->0x8, p2->0xffffffff , p3-> 0x8000000 11 | result: .space 16 12 | ################################################# 13 | # # 14 | # text segment # 15 | # # 16 | ################################################# 17 | 18 | .text 19 | .globl __start 20 | #read first int and move it to a temp reg 21 | __start: 22 | 23 | lw $t0, num1 24 | lw $t1, num2 25 | 26 | ####multiplication 27 | mult $t0 , $t1 28 | mflo $t2 #low register has the product of the multiplication 29 | sw $t2, result #store result 30 | mfhi $t3 #high register has the high-order word(extra result) 31 | sw $t3, result + 4 32 | 33 | ####multiplication unsigned 34 | multu $t0 , $t1 35 | mflo $t4 36 | sw $t4, result + 8 37 | mfhi $t5 38 | sw $t5, result + 12 39 | 40 | Exit: li $v0, 10 41 | syscall #au revoir... -------------------------------------------------------------------------------- /lab-exercises/2/q2_7/README.md: -------------------------------------------------------------------------------- 1 | # question 2.7 2 | multiplication (hi is upperword result, lo lowerword result) -------------------------------------------------------------------------------- /lab-exercises/2/q2_8/2_8.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | dividend: .asciiz "Enter dividend: " 9 | divisor: .asciiz "Enter divisor: " 10 | endl: .asciiz "\n" 11 | quotient: .asciiz "quotient is: " 12 | remainder: .asciiz "remainder is: " 13 | 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | # Input 25 | la $a0,dividend 26 | li $v0,4 27 | syscall # prompt for dividend 28 | li $v0,5 29 | syscall # read dividend 30 | move $t0,$v0 # dividend in $t0 31 | 32 | move $a0, $t0 #print int 33 | li $v0 , 1 34 | syscall 35 | 36 | la $a0,endl # system call to print 37 | li $v0, 4 # out a newline 38 | syscall 39 | 40 | la $a0,divisor 41 | li $v0,4 42 | syscall # prompt for divisor 43 | li $v0,5 44 | syscall # read divisor 45 | move $t1,$v0 # divisor in $t1 46 | 47 | move $a0, $t1 #print int 48 | li $v0 , 1 49 | syscall 50 | 51 | la $a0,endl # system call to print 52 | li $v0, 4 # out a newline 53 | syscall 54 | 55 | #Calculations 56 | div $t4, $t0, $t1 #quotient in t4 57 | mfhi $t5 #remainder in $t5 58 | #Output 59 | la $a0,quotient 60 | li $v0,4 61 | syscall # display "quotient is :" 62 | move $a0,$t4 63 | li $v0,1 64 | syscall # display quotient 65 | la $a0,endl 66 | li $v0,4 67 | syscall # newline 68 | la $a0,remainder 69 | 70 | 71 | li $v0,4 72 | syscall # display "remainder is :" 73 | move $a0,$t5 74 | li $v0,1 75 | syscall # display remainder 76 | la $a0,endl 77 | li $v0,4 78 | syscall # newline 79 | 80 | Exit: li $v0, 10 81 | syscall #au revoir... -------------------------------------------------------------------------------- /lab-exercises/2/q2_8/README.md: -------------------------------------------------------------------------------- 1 | # question 2.8 2 | division (quotient in lo, remainder in hi) -------------------------------------------------------------------------------- /lab-exercises/3/q3_1/3_1_a.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | enter: .asciiz "Enter an integer:\n" 10 | msg1: .asciiz "\nIs divided with 2.\n" 11 | msg2: .asciiz "\nIs divided with 3.\n" 12 | msg3: .asciiz "\nIs divided with 5.\n" 13 | msg4: .asciiz "\nIs not divided with 2, 3 or 5.\n" 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | li $v0, 4 25 | la $a0, enter # display "enter an integer:" 26 | syscall 27 | 28 | j read_int 29 | li $t9 , 0 30 | li $t8 , 0 31 | read_int: li $v0, 5 # $v0 <--- integer 32 | syscall 33 | move $t0 , $v0 34 | j print_int_from_reg 35 | 36 | print_int_from_reg: move $a0, $t0 # ??? --> register with integer 37 | li $v0, 1 38 | syscall 39 | j switch_case_no_break 40 | 41 | switch_case_no_break: 42 | li $t2 , 2 43 | li $t3 , 3 44 | li $t5 , 5 45 | case01: div $t0 , $t2 46 | mfhi $t1 47 | bne $t1 , $zero , case02 48 | if_case01: li $v0, 4 49 | la $a0, msg1 50 | syscall 51 | li $t9, 1 52 | 53 | case02: div $t0 , $t3 54 | mfhi $t1 55 | bne $t1 , $zero , case03 56 | if_case02: li $v0, 4 57 | la $a0, msg2 58 | syscall 59 | li $t9, 1 60 | 61 | case03: div $t0 , $t5 62 | mfhi $t1 63 | bne $t1 , $zero , else_case 64 | if_case03: li $v0, 4 65 | la $a0, msg3 66 | syscall 67 | li $t9, 1 68 | 69 | else_case: bne $t9 , $t8 , Exit 70 | li $v0, 4 71 | la $a0, msg4 72 | syscall 73 | 74 | j Exit 75 | 76 | Exit: li $v0, 10 77 | syscall #au revoir... 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /lab-exercises/3/q3_1/3_1_b.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | enter: .asciiz "Enter an integer:\n" 10 | msg1: .asciiz "\nIs divided with 2.\n" 11 | msg2: .asciiz "\nIs divided with 3.\n" 12 | msg3: .asciiz "\nIs divided with 5.\n" 13 | msg4: .asciiz "\nIs not divided with 2, 3 or 5.\n" 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | li $v0, 4 25 | la $a0, enter # display "enter an integer:" 26 | syscall 27 | 28 | read_int: li $v0, 5 # $v0 <--- integer 29 | syscall 30 | move $t0 , $v0 31 | j print_int_from_reg 32 | 33 | print_int_from_reg: move $a0, $t0 # ??? --> register with integer 34 | li $v0, 1 35 | syscall 36 | j switch_case_no_break 37 | 38 | switch_case_no_break: li $t9 , 0 39 | li $t8 , 0 40 | li $t2 , 2 41 | li $t3 , 3 42 | li $t5 , 5 43 | case01: div $t0 , $t2 44 | mfhi $t1 45 | bne $t1 , $zero ,case02 46 | if_case01: li $v0, 4 47 | la $a0, msg1 48 | syscall 49 | li $t9, 1 50 | j Exit 51 | 52 | case02: div $t0 , $t3 53 | mfhi $t1 54 | bne $t1 , $zero , case03 55 | if_case02: li $v0, 4 56 | la $a0, msg2 57 | syscall 58 | li $t9, 1 59 | j Exit 60 | 61 | case03: div $t0 , $t5 62 | mfhi $t1 63 | bne $t1 , $zero , Exit 64 | if_case03: li $v0, 4 65 | la $a0, msg3 66 | syscall 67 | li $t9, 1 68 | 69 | else_case: bne $t9 , $t8 , Exit 70 | li $v0, 4 71 | la $a0, msg4 72 | syscall 73 | 74 | j Exit 75 | 76 | Exit: li $v0, 10 77 | syscall #au revoir... 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /lab-exercises/3/q3_1/README.md: -------------------------------------------------------------------------------- 1 | # question 3.1 2 | switch cases -------------------------------------------------------------------------------- /lab-exercises/3/q3_2/3_2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | word1: .word 0x11001011 10 | word2: .word 0x11010010 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | li $s0, 0 #distance calculator 22 | li $s1, 1 #counter 23 | li $s2, 32 24 | sw $t8, word1 25 | sw $t9, word2 26 | la $t0, word1 27 | la $t1, word2 28 | 29 | loop: 30 | lb $t2, word1 31 | lb $t3, word2 32 | xor $t4, $t2, $t3 33 | add $s0, $s0, $t4 34 | srl $t8, $t8, 1 35 | srl $t9, $t9, 1 36 | addi $s1, 1 37 | ble $s1, $s2, loop 38 | 39 | Exit: li $v0, 10 40 | syscall #au revoir... 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /lab-exercises/3/q3_2/README.md: -------------------------------------------------------------------------------- 1 | # question 3.2 2 | for loop, not finished(problem loading first bytes...) -------------------------------------------------------------------------------- /lab-exercises/3/q3_3/3_3_a.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | string: .asciiz "This is string is to be read:)\n" 10 | copy: .space 80 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | li $t1,0 # counter for string 22 | li $t9, 13 #n is max values read ###################change ############### 23 | li $s0,'\n' # character to end copy 24 | 25 | while: lbu $t0,string($t1) # load a character 26 | beq $t0,$s0,end # if character to end copy then exit loop 27 | bgt $t1, $t9, end 28 | sb $t0,copy($t1) # copy character 29 | addi $t1,$t1,1 # increment counter 30 | j while # repeat while loop 31 | end: li $t2,0 32 | sb $t2,copy($t1) # append end character to copied string 33 | la $a0,copy # display copy 34 | li $v0,4 35 | syscall 36 | j Exit 37 | 38 | 39 | Exit: li $v0, 10 40 | syscall #au revoir... 41 | -------------------------------------------------------------------------------- /lab-exercises/3/q3_3/3_3_b.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | string: .asciiz "This is string is to be read:)\n" 10 | copy: .space 80 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | li $t1,0 # counter for string 22 | li $t9, 13 #n is max values read ###################change ############### 23 | li $s0,'g' # character to end copy 24 | 25 | while: lbu $t0,string($t1) # load a character 26 | beq $t0,$s0,condition # if character to end copy then exit loop 27 | 28 | continue:sb $t0,copy($t1) # copy character 29 | addi $t1,$t1,1 # increment counter 30 | j while # repeat while loop 31 | end: li $t2,0 32 | sb $t2,copy($t1) # append end character to copied string 33 | la $a0,copy # display copy 34 | li $v0,4 35 | syscall 36 | j Exit 37 | 38 | condition: bgt $t1, $t9, continue 39 | j end 40 | 41 | Exit: li $v0, 10 42 | syscall #au revoir... 43 | -------------------------------------------------------------------------------- /lab-exercises/3/q3_3/README.md: -------------------------------------------------------------------------------- 1 | # question 3.3 2 | changes in or and and conditions in loop -------------------------------------------------------------------------------- /lab-exercises/3/q3_4/3_4.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str: .space 50 10 | msg: .asciiz "Give a string or 0 to finish.\n" 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | addi $t0, 1 # t0 is the counter 22 | addi $t1, 10 23 | 24 | 25 | loop: li $v0, 4 26 | la $a0, msg # ??? --> label with string 27 | syscall 28 | 29 | li $v0, 8 # code to read a string 30 | la $a0, str # ??? --> label with string 31 | li $a1, 51 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 32 | syscall 33 | 34 | la $s0, str 35 | #addi $t1, 0 # t1 is the inside counter 36 | loop2: lbu $t2, 0($s0) 37 | beq $t2, $t1, exit #if zero exit 38 | li $t9, 122 39 | bgt $t2, $t9, step # if $t2 > 122 next 40 | li $t9, 97 41 | blt $t2, $t9, step # if less than 97 next 42 | li $t9, 32 # then its capital 43 | sub $t2, $t2, $t9 #and convert 44 | sb $t2,0($s0) 45 | step:addi $s0,$s0, 1 46 | j loop2 47 | exit: 48 | 49 | li $v0, 4 50 | la $a0, str # ??? --> label with string 51 | syscall 52 | 53 | 54 | Exit: li $v0, 10 55 | syscall #au revoir... 56 | -------------------------------------------------------------------------------- /lab-exercises/4/q4_1/4_1_a_d.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # lab4_1a.s # 4 | # stack exercise 1 (to be completed) # 5 | # # 6 | ################################################# 7 | ################################################# 8 | # # 9 | # data segment # 10 | # # 11 | ################################################# 12 | .data 13 | max: .asciiz "Max is : " 14 | min: .asciiz "Min is : " 15 | endl: .asciiz "\n" 16 | ################################################# 17 | # # 18 | # text segment # 19 | # # 20 | ################################################# 21 | .text 22 | .globl __start 23 | __start: 24 | # start of main program 25 | li $a0,-10 # Initialize variables 26 | li $a1,-30 27 | li $a2,120 28 | li $a3,200 29 | 30 | jal minmax 31 | 32 | move $t0,$v0 33 | move $t1,$v1 34 | la $a0,max 35 | li $v0,4 36 | syscall # display "Max is :" 37 | move $a0,$t0 38 | li $v0,1 39 | syscall # display max 40 | la $a0,endl 41 | li $v0,4 42 | syscall # display end of line 43 | la $a0,min 44 | li $v0,4 45 | syscall # display "Min is :" 46 | move $a0,$t1 47 | li $v0,1 48 | syscall # display min 49 | la $a0,endl 50 | li $v0,4 51 | syscall # display end of line 52 | li $v0,10 53 | syscall # exit 54 | # end of main program 55 | # start of procedure 56 | minmax: addi $sp,$sp,-8 57 | sw $s0,0($sp) 58 | sw $s1,4($sp) 59 | 60 | min1: move $s0,$a0 #hold min value 61 | 62 | cont4: bge $s0,$a1,cont5 63 | move $s0,$a1 64 | 65 | cont5: bge $s0,$a2,cont6 66 | move $s0,$a2 67 | 68 | cont6: bge $s0,$a3,resMin 69 | move $s0,$a3 70 | 71 | resMin: move $v0,$s0 72 | 73 | max1: move $s1,$a0 #hold max value 74 | 75 | ble $s1,$a1,cont2 76 | move $s1,$a2 77 | 78 | cont2: ble $s1,$a2,cont3 79 | move $s1,$a2 80 | 81 | cont3: ble $s1,$a3,resMax 82 | move $s1,$a3 83 | 84 | resMax: move $v1,$s1 85 | 86 | lw $s0,0($sp) 87 | lw $s1,4($sp) 88 | addi $sp,$sp,8 89 | 90 | jr $ra 91 | # end of procedure 92 | 93 | ################################################# 94 | # # 95 | # End of program # 96 | # # 97 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/4/q4_1/4_1_a_m.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # lab4_1a.s # 4 | # stack exercise 1 (to be completed) # 5 | # # 6 | ################################################# 7 | .text 8 | .globl __start 9 | __start: 10 | # start of main program 11 | li $a0,-10 # Initialize variables 12 | li $a1,-30 # 13 | li $a2,120 # 14 | li $a3,200 # 15 | 16 | jal minmax 17 | 18 | move $t0,$v0 19 | move $t1,$v1 20 | la $a0,max 21 | li $v0,4 22 | syscall # display "Max is :" 23 | move $a0,$t0 24 | li $v0,1 25 | syscall # display max 26 | la $a0,endl 27 | li $v0,4 28 | syscall # display end of line 29 | la $a0,min 30 | li $v0,4 31 | syscall # display "Min is :" 32 | move $a0,$t1 33 | li $v0,1 34 | syscall # display min 35 | la $a0,endl 36 | li $v0,4 37 | syscall # display end of line 38 | li $v0,10 39 | syscall # exit 40 | # end of main program 41 | # start of procedure 42 | minmax: 43 | #max 44 | move $v0, $a0 #v0 -> max 45 | first_comp: 46 | ble $a1,$v0,second_comp 47 | move $v0,$a1 48 | second_comp: 49 | ble $a2,$v0,third_comp 50 | move $v0,$a2 51 | third_comp: 52 | ble $a3,$v0, first 53 | move $v0,$a3 54 | #min 55 | first: 56 | move $v1, $a0 57 | bge $a1,$v1,second 58 | move $v1,$a1 59 | second: 60 | bge $a2,$v1,third 61 | move $v1,$a2 62 | third: 63 | bge $a3,$v1, exit 64 | move $v1,$a3 65 | exit: 66 | jr $ra 67 | # end of procedure 68 | .data 69 | max: .asciiz "Max is : " 70 | min: .asciiz "Min is : " 71 | endl: .asciiz "\n" 72 | ################################################# 73 | # # 74 | # End of program # 75 | # # 76 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/4/q4_1/README.md: -------------------------------------------------------------------------------- 1 | # question 4.1 2 | create a procedure, that gets 4 variables through the $a0, $a1, $a2, $a3 registers and saves the min in $v0 and the max in $v1. if the results aren't absolute, use the $s registers. 3 | (2 different versions of the program; one Efi's and one mine) -------------------------------------------------------------------------------- /lab-exercises/4/q4_2/4_2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | .data 7 | prompt: .asciiz "Enter integer number: " 8 | endl: .asciiz "\n" 9 | ################################################# 10 | # # 11 | # text segment # 12 | # # 13 | ################################################# 14 | 15 | .text 16 | .globl __start 17 | __start: 18 | #-------------------------start of main program----------------------------------------- 19 | la $a0,prompt 20 | li $v0,4 21 | syscall # display "Enter integer number :" 22 | li $v0,5 23 | syscall # read integer 24 | move $t0,$v0 # $t0 -> integer 25 | la $a0,endl 26 | li $v0,4 27 | syscall # display end of line 28 | move $a0,$t0 #a0 -> integer 29 | 30 | jal rec 31 | 32 | li $v0,10 33 | syscall # exit 34 | #------------------------ end of main program-------------------- 35 | 36 | 37 | 38 | #------------------------- start of procedure-------------------- 39 | rec: 40 | addi $sp, -4 41 | sw $ra, 0($sp) 42 | 43 | li $v0, 1 # print the integer 44 | syscall 45 | 46 | move $t1, $a0 47 | la $a0, endl #new line 48 | li $v0, 4 49 | syscall 50 | 51 | move $a0, $t1 52 | 53 | beq $a0, $zero, end 54 | 55 | l1: 56 | addi $a0, -1 57 | jal rec #recursion 58 | 59 | end: #base case 60 | lw $ra, 0($sp) 61 | addi $sp, 4 62 | jr $ra 63 | 64 | #------------------------- end of procedure------------------------ 65 | 66 | print_endl: la $a0,endl # system call to print 67 | li $v0, 4 # out a newline 68 | syscall 69 | jr $ra 70 | 71 | printInt: li $v0, 1 72 | syscall 73 | jr $ra -------------------------------------------------------------------------------- /lab-exercises/4/q4_2/README.md: -------------------------------------------------------------------------------- 1 | # question 4.2 2 | ## simple recursion 3 | 2 parts of the program: 4 | * the main program, that takes an integer and calls the recursive function with the int as an arguement 5 | * the *recursive function*, that decreases the number and prints it until the base case -------------------------------------------------------------------------------- /lab-exercises/4/q4_3/4_3.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | prompt: .asciiz "Enter integer number: " 9 | memused: .asciiz "Stack bytes used: " 10 | endl: .asciiz "\n" 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | # start of main program 22 | 23 | la $a0, prompt 24 | li $v0, 4 25 | syscall # display "Enter integer number: " 26 | 27 | li $v0, 5 28 | syscall # read integer 29 | 30 | move $t0, $v0 31 | move $a0, $t0 #$a0 has the integer to be print 32 | 33 | li $v0, 1 34 | syscall # display integer 35 | 36 | la $a0, endl 37 | li $v0,4 38 | syscall # display end of line 39 | 40 | move $a0, $t0 41 | 42 | move $v0, $sp 43 | move $s0, $sp 44 | jal rec_countdown 45 | 46 | sub $s0, $s0, $v0 47 | la $a0, memused 48 | li $v0, 4 49 | syscall # display "Stack bytes used: " 50 | move $a0, $s0 51 | li $v0, 1 52 | syscall # display memory used 53 | la $a0, endl 54 | li $v0,4 55 | syscall # display end of line 56 | 57 | li $v0, 10 58 | syscall # exit 59 | 60 | # end of main program 61 | 62 | # start of procedure 63 | rec_countdown: 64 | addi $sp, $sp, -4 65 | sw $ra, 0($sp) 66 | ble $a0, $zero, return 67 | addi $a0, $a0, -1 68 | jal rec_countdown 69 | return: 70 | move $t0, $v0 71 | li $v0, 1 72 | syscall # display argument 73 | move $t1, $a0 74 | la $a0, endl 75 | li $v0,4 76 | syscall # display end of line 77 | move $v0, $t0 78 | move $a0, $t1 79 | bge $sp, $v0, nothing 80 | move $v0, $sp 81 | nothing: 82 | addi $a0, $a0, 1 83 | lw $ra, 0($sp) 84 | addi $sp, $sp, 4 85 | jr $ra 86 | 87 | # end of procedure 88 | 89 | Exit: li $v0, 10 90 | syscall #au revoir... 91 | -------------------------------------------------------------------------------- /lab-exercises/4/q4_3/README.md: -------------------------------------------------------------------------------- 1 | # question 4.3 2 | ## recursion & return an arguement 3 | 2 parts of the program: 4 | * the main program, that takes an integer and calls the recursive function with the int as an arguement 5 | * the *recursive function*, that decreases the number and prints it until the base case 6 | * at the end of the recusrion, it returns the ammount of stack space used -------------------------------------------------------------------------------- /lab-exercises/5/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tonykalantzis/assembly-MIPS-code-examples/e15544c23aeb6dbe9dbb7b25a7b6dae7687f6213/lab-exercises/5/README.md -------------------------------------------------------------------------------- /lab-exercises/5/q5_1/5_1.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | 10 | ################################################# 11 | # # 12 | # text segment # 13 | # # 14 | ################################################# 15 | 16 | .text 17 | .globl __start 18 | #read first int and move it to a temp reg 19 | __start: 20 | read_float: li $v0, 6 # $f0 <--- float 21 | syscall 22 | j print_float_from_reg 23 | 24 | print_float_from_reg: mov.s $f12, $f0 # ??? --> FP register with float 25 | li $v0, 2 26 | syscall 27 | j Exit 28 | 29 | Exit: li $v0, 10 30 | syscall #au revoir... 31 | 32 | ################################################# 33 | # # 34 | # End of program # 35 | # # 36 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/5/q5_1/README.md: -------------------------------------------------------------------------------- 1 | # question 5.1 2 | trying out the read_float and print_float system calls 3 | -------------------------------------------------------------------------------- /lab-exercises/5/q5_2/5_2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | zer_f: .float 0.0 10 | m_inf: .word 0xff800000 #-infinite 11 | p_inf: .word 0x7f800000 #+infinite 12 | nan: .word 0x7fffffff #+NaN 13 | x: .float 55.0 14 | y: .float -55.0 15 | 16 | ################################################# 17 | # # 18 | # text segment # 19 | # # 20 | ################################################# 21 | 22 | .text 23 | .globl __start 24 | __start: 25 | #-------------start of main program-------------# 26 | main: l.s $f0, x 27 | l.s $f1, y 28 | mul.s $f12,$f0,$f1 29 | 30 | print_1st: jal print_float 31 | jal print_endl 32 | 33 | l.s $f0, x 34 | l.s $f1, m_inf 35 | mul.s $f12, $f0,$f1 36 | 37 | print_2nd: jal print_float 38 | jal print_endl 39 | 40 | l.s $f0, y 41 | l.s $f1, zer_f 42 | div.s $f12, $f0, $f1 43 | 44 | print_3rd: jal print_float 45 | jal print_endl 46 | 47 | l.s $f0,zer_f 48 | l.s $f1,zer_f 49 | div.s $f12, $f0, $f1 50 | 51 | print_4rd: jal print_float 52 | jal print_endl 53 | 54 | l.s $f0, zer_f 55 | l.s $f1, p_inf 56 | mul.s $f12, $f0, $f1 57 | 58 | print_5th: jal print_float 59 | jal print_endl 60 | 61 | l.s $f0,p_inf 62 | l.s $f1, m_inf 63 | div.s $f12, $f0, $f1 64 | 65 | print_6th: jal print_float 66 | jal print_endl 67 | 68 | add.s $f12, $f0, $f1 69 | 70 | print_7th: jal print_float 71 | jal print_endl 72 | 73 | l.s $f0, x 74 | l.s $f1, nan 75 | add.s $f12, $f0 , $f1 76 | 77 | print_8th: jal print_float 78 | jal print_endl 79 | 80 | Exit: li $v0, 10 81 | syscall #au revoir... 82 | #-------------end of main program--------------# 83 | 84 | #-------------start of procedure--------------# 85 | print_float: li $v0, 2 86 | syscall 87 | jr $ra 88 | 89 | print_endl: la $a0,endl 90 | li $v0, 4 91 | syscall 92 | jr $ra 93 | 94 | #-------------end of procedure---------------# 95 | 96 | -------------------------------------------------------------------------------- /lab-exercises/5/q5_2/README.md: -------------------------------------------------------------------------------- 1 | # question 5.2 2 | save: 0.0, +∞, -∞, +NaN, and 2 number x (x>= 0), y(y<0). 3 | try out these calculations: 4 | x*y 5 | x*(-∞) 6 | y/0 7 | 0/0 8 | 0*(+∞) 9 | (+∞)/(-∞) 10 | (+∞)+(-∞) 11 | x+NaN -------------------------------------------------------------------------------- /lab-exercises/5/q5_3/5_3_a.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # lab5_3a.s # 4 | # Integer factorial # 5 | # # 6 | ################################################# 7 | .text 8 | .globl __start 9 | __start: 10 | la $a0,n 11 | lw $t0,0($a0) # $t0 = n 12 | li $t2,1 # $t2 index i=1..n 13 | li $t1,1 # $t1 contains i! 14 | loop: mul $t1,$t1,$t2 15 | move $a0,$t2 16 | li $v0,1 17 | syscall # display i 18 | la $a0,msg1 19 | li $v0,4 20 | syscall # display "! is :" 21 | move $a0,$t1 22 | li $v0,1 23 | syscall # display i! 24 | la $a0,endl 25 | li $v0,4 26 | syscall # print end of line 27 | addi $t2,$t2,1 # i=i+1 28 | ble $t2,$t0,loop # repeat if i<=n 29 | li $v0,10 # exit 30 | syscall 31 | .data 32 | n: .word 25 33 | msg1: .asciiz "! is :" 34 | endl: .asciiz "\n" 35 | ################################################# 36 | # # 37 | # End of program # 38 | # # 39 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/5/q5_3/5_3_b.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # lab5_3a.s # 4 | # Integer factorial # 5 | # # 6 | ################################################# 7 | .data 8 | n: .word 25 9 | msg1: .asciiz "! is: " 10 | endl: .asciiz "\n" 11 | 12 | .text 13 | .globl __start 14 | __start: 15 | la $a0,n 16 | lw $t0,0($a0) # $t0 = n 17 | li.s $f1, 1.0 # $t2 index i=1..n 18 | li $t2, 1 # $t1 contains i! 19 | 20 | loop: mtc1 $t2, $f2 21 | cvt.s.w $f2, $f2 22 | mul.s $f1, $f1, $f2 23 | 24 | move $a0,$t2 #display i 25 | jal print_int 26 | 27 | la $a0,msg1 #display ! is: 28 | jal print_string 29 | 30 | mov.s $f12, $f1 31 | jal print_float 32 | 33 | jal print_endl 34 | 35 | addi $t2,$t2,1 # i=i+1 36 | ble $t2,$t0,loop # repeat if i<=n 37 | 38 | 39 | li $v0,10 # exit 40 | syscall 41 | ######## procedures ############ 42 | print_string: li $v0, 4 43 | syscall 44 | jr $ra 45 | 46 | print_int: li $v0, 1 47 | syscall 48 | jr $ra 49 | 50 | print_endl: la $a0,endl 51 | li $v0, 4 #system call to print 52 | syscall 53 | jr $ra 54 | 55 | print_float: li $v0, 2 56 | syscall 57 | jr $ra 58 | ################################################# 59 | # # 60 | # End of program # 61 | # # 62 | ################################################# -------------------------------------------------------------------------------- /lab-exercises/5/q5_3/README.md: -------------------------------------------------------------------------------- 1 | # 5_3 2 | 3 | ## 5.3.1 4 | calculating all the factorials of the number inserted 5 | 6 | ## 5.3.2 7 | the same as 5.3.1 but with floating point single precision -------------------------------------------------------------------------------- /lab-exercises/README.md: -------------------------------------------------------------------------------- 1 | # Exercises of the lab of Architecture Of Computers 2 | 3 | This lab is about MIPS assembly in the lab of architecture of computer. It is teached during the 2nd semester of the degree: Informatics & Telecommunications, University Of Athens. 4 | 5 | -------------------------------------------------------------------------------- /previous-exams/README.md: -------------------------------------------------------------------------------- 1 | ## Previous exams and some exercises for practice 2 | -------------------------------------------------------------------------------- /previous-exams/exams/CommonCharsin2Strings.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str1: .space 11 10 | str2: .space 11 11 | mess: .asciiz "Give a string of 10 characters: " 12 | position1: .asciiz "Position of character in the first string: " 13 | position2: .asciiz "Position of character in the second string: " 14 | 15 | ################################################# 16 | # # 17 | # text segment # 18 | # # 19 | ################################################# 20 | .text 21 | .globl __start 22 | __start: 23 | #-------------start of main program-------------# 24 | la $a0, mess 25 | jal print_string 26 | 27 | read_string1: li $v0, 8 #code to read a string 28 | la $a0,str1 #??? --> label with string 29 | li $a1,11 #n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 30 | syscall 31 | 32 | print_string1: li $v0, 4 33 | la $a0, str1 #str1--> label with string1 34 | syscall 35 | 36 | jal print_endl 37 | 38 | la $a0, mess 39 | jal print_string 40 | 41 | read_string2: li $v0, 8 #code to read a string 42 | la $a0,str2 #??? --> label with string 43 | li $a1,11 #n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 44 | syscall 45 | 46 | print_string2: li $v0, 4 47 | la $a0, str2 #str2 --> label with string2 48 | syscall 49 | 50 | jal print_endl 51 | 52 | li $t1,0 #counter of str1-- 53 | la $s0, str1 #$s0-> string 54 | li $t5, 10 #constant 55 | 56 | isolation: lbu $t0, 0($s0) #$t0 has the char of str1 57 | li $t2, 0 #counter of str2 58 | la $s1, str2 #$s1-> string 59 | search_str2: lbu $t3,0($s1) #$t3 has the char of str2 60 | bne $t3,$t0,next_char 61 | 62 | move $a0,$t3 #print the mutual char 63 | jal print_char 64 | jal print_endl 65 | 66 | la $a0, position1 #print message 67 | jal print_string 68 | 69 | move $a0, $t1 70 | jal print_int #print position 71 | jal print_endl 72 | 73 | la $a0,position2 #print message 74 | jal print_string 75 | move $a0, $t2 76 | jal print_int #print position 77 | jal print_endl 78 | 79 | next_char: addi $s1,$s1,1 80 | addi $t2, $t2, 1 81 | beq $t2,$t5,next_str1 82 | j search_str2 83 | 84 | next_str1: add $s0, $s0,1 85 | addi $t1,$t1,1 86 | beq $t1,$t5,exit #if $t1==10 exit 87 | j isolation #next char of str1 88 | 89 | exit: li $v0, 10 90 | syscall #au revoir... 91 | #-------------end of main program--------------# 92 | 93 | #-------------start of procedures--------------# 94 | print_endl: la $a0,endl 95 | li $v0, 4 #system call to print 96 | syscall 97 | jr $ra 98 | 99 | print_string: li $v0, 4 100 | syscall 101 | jr $ra 102 | 103 | print_char: li $v0,11 104 | syscall 105 | jr $ra 106 | 107 | print_int: li $v0, 1 108 | syscall 109 | jr $ra 110 | 111 | #-------------end of procedures---------------# 112 | 113 | 114 | -------------------------------------------------------------------------------- /previous-exams/exams/MultOFVectorsFloat.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | bye: .asciiz "\nbye:)" 10 | parenthesi1: .asciiz "[ " 11 | parenthesi2: .asciiz " ]" 12 | space: .asciiz " " 13 | epi: .asciiz " * " 14 | ison: .asciiz " = " 15 | 16 | #some different examples to try out 17 | #just uncomment the one and comment the previous 18 | vector1: .byte 1, 2, 3, 4, 0, 1, 2, 3, 4, 0 19 | vector2: .byte 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 #result 20 20 | 21 | #vector1: .byte 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 22 | #vector2: .byte 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 #result 10 23 | 24 | #vector1: .byte 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 25 | #vector2: .byte 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 #result 0 26 | 27 | #vector1: .byte 1, 0, 0, 0, 6, 0, 2, 3, 0, 4 28 | #vector2: .byte 1, 0, 1, 2, 7, 1, 1, 1, 1, 1 #result 52 29 | 30 | ################################################# 31 | # # 32 | # text segment # 33 | # # 34 | ################################################# 35 | 36 | .text 37 | .globl __start 38 | #read first int and move it to a temp reg 39 | __start: 40 | #-----------PRINT THE VECTOR1----------------------------------- 41 | li $v0, 4 42 | la $a0, parenthesi1 43 | syscall 44 | 45 | la $s0, vector1 46 | li $t0, 0 #counter 47 | li $t9, 10 #when to stop counter 48 | 49 | loop1: lbu $t1, 0($s0) #load the ("%d", $t0) character 50 | move $a0, $t1 51 | li $v0, 1 52 | syscall 53 | 54 | addi $s0, $s0, 1 #next number 55 | addi $t0, $t0, 1 #increase counter 56 | beq $t0, $t9, exit_loop1 57 | li $v0, 4 58 | la $a0, space 59 | syscall 60 | j loop1 61 | exit_loop1: li $v0, 4 62 | la $a0, parenthesi2 63 | syscall 64 | 65 | #-----------PRINT THE *--------------------------------------- 66 | li $v0, 4 67 | la $a0, epi 68 | syscall 69 | #-----------PRINT THE VECTOR2--------------------------------- 70 | li $v0, 4 71 | la $a0, parenthesi1 72 | syscall 73 | 74 | la $s0, vector2 75 | li $t0, 0 76 | li $t9, 10 77 | 78 | loop2: lbu $t1, 0($s0) #load the ("%d", $t0) character 79 | move $a0, $t1 80 | li $v0, 1 81 | syscall 82 | 83 | addi $s0, $s0, 1 #next number 84 | addi $t0, $t0, 1 #increase counter 85 | beq $t0, $t9, exit_loop2 86 | li $v0, 4 87 | la $a0, space 88 | syscall 89 | j loop2 90 | exit_loop2: li $v0, 4 91 | la $a0, parenthesi2 92 | syscall 93 | #------------------PRINT ISON--------------------------------- 94 | li $v0, 4 95 | la $a0, ison 96 | syscall 97 | #------------------CALCULATE SUM------------------------------ 98 | la $s0, vector1 99 | la $s1, vector2 100 | li $t0, 0 101 | li $t9, 10 102 | li $s5, 0 # $s5 is the sum 103 | loop3: lbu $t1, 0($s0) 104 | lbu $t2, 0($s1) 105 | mult $t1, $t2 106 | mflo $t3 #t3 the final result! 107 | add $t5, $t5, $t3 108 | 109 | addi $s0, $s0, 1 110 | addi $s1, $s1, 1 111 | addi $t0, $t0, 1 112 | beq $t0, $t9, exit_loop3 113 | j loop3 114 | exit_loop3: 115 | #-----------------PRINT SUM----------------------------------- 116 | move $a0, $t5 117 | li $v0, 1 118 | syscall 119 | j Exit 120 | Exit: la $a0, bye 121 | li $v0, 4 122 | syscall 123 | li $v0, 10 124 | syscall #au revoir... 125 | -------------------------------------------------------------------------------- /previous-exams/exams/README.md: -------------------------------------------------------------------------------- 1 | # Exam 2 | ### Description for each exercise 3 | 4 | - **CommonCharsin2Strings.s** 5 | 6 | Write a program in assembly MIPS that takes two strings of 10 characters from the user and prints for each character of the first string how many times appears in the second string. In addition, the program prints the position of the character in the string each time. 7 | 8 | - **MultOfVectorsFloat.s** 9 | 10 | Write a program in assembly MIPS that has two vectors with 10 elements, who are saved in the data segment and after printing them, calculates and prints their result. 11 | Example: [1 2 3 4 0 1 2 3 4 0] * [1 1 1 1 1 1 1 1 1 1] = 20 12 | 13 | - **SecondDegreeEquationFloat.s** 14 | 15 | Write a program in assembly MIPS that takes the a, b, c of the ax^2 + bx + c = 0 as floating point numbers and prints the solution/s or that it has not any solutions based on the Δ = b^2 - 4ac. 16 | 17 | -------------------------------------------------------------------------------- /previous-exams/exams/SecondDegreeEquationFloat.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msga: .asciiz "Give number a: " 10 | msgb: .asciiz "Give number b: " 11 | msgc: .asciiz "Give number c: " 12 | error: .asciiz "It has no solutions :/\n" 13 | msg0: .asciiz "x = " 14 | msg1: .asciiz "x1 = " 15 | msg2: .asciiz "x2 = " 16 | ################################################# 17 | # # 18 | # text segment # 19 | # # 20 | ################################################# 21 | 22 | .text 23 | .globl __start 24 | #read first int and move it to a temp reg 25 | __start: 26 | #----------------------------A-------------------------------------- 27 | li $v0, 4 #print string 28 | la $a0, msga 29 | syscall 30 | 31 | li $v0, 6 #read float 32 | syscall 33 | mov.s $f1, $f0 #$f1 = a 34 | 35 | mov.s $f12, $f1 #print float from reg 36 | li $v0, 2 37 | syscall 38 | 39 | la $a0,endl # print end of line 40 | li $v0, 4 41 | syscall 42 | #----------------------------B-------------------------------------- 43 | 44 | li $v0, 4 #print string 45 | la $a0, msgb 46 | syscall 47 | 48 | li $v0, 6 #read float 49 | syscall 50 | mov.s $f2, $f0 # $f2 = b 51 | 52 | mov.s $f12, $f2 #print float from reg 53 | li $v0, 2 54 | syscall 55 | 56 | la $a0,endl # print end of line 57 | li $v0, 4 58 | syscall 59 | #----------------------------C------------------------------------- 60 | 61 | li $v0, 4 #print string 62 | la $a0, msgc 63 | syscall 64 | 65 | li $v0, 6 #read float 66 | syscall 67 | mov.s $f3, $f0 #$f3 = c 68 | 69 | mov.s $f12, $f3 #print float from reg 70 | li $v0, 2 71 | syscall 72 | 73 | la $a0,endl # print end of line 74 | li $v0, 4 75 | syscall 76 | #----------------------------------------------------------------- 77 | li.s $f10, 4.0 78 | 79 | mul.s $f4, $f2, $f2 #b^2 80 | mul.s $f5, $f1, $f3 #a*c 81 | mul.s $f5, $f5, $f10 #4*a*c 82 | 83 | sub.s $f6, $f4, $f5 #$f6 = Δ = b^2 - 4ac 84 | 85 | #---------------------------------------------------------------------- 86 | 87 | switch_case_with_break: li.s $f8, 0.0 88 | li.s $f9, -1.0 89 | li.s $f10, 2.0 90 | case001: c.eq.s $f6, $f8 #if Δ = 0 91 | bc1f case002 #false then jump to case002 92 | mul.s $f2, $f2, $f9 #-b 93 | mul.s $f1, $f1, $f10 #2a 94 | div.s $f0, $f2, $f1 #-b/2a 95 | 96 | la $a0, msg0 97 | li $v0, 4 98 | syscall 99 | 100 | mov.s $f12, $f0 101 | li $v0, 2 102 | syscall 103 | j Exit 104 | 105 | case002: c.lt.s $f6, $f8 #if Δ < 0 106 | bc1f else_case 107 | li $v0, 4 108 | la $a0, error 109 | syscall 110 | j Exit 111 | 112 | else_case: sqrt.s $f7, $f6 # sqrt(Δ) 113 | 114 | mul.s $f2, $f2, $f9 # -b # 115 | mul.s $f1, $f1, $f10 # 2a # 116 | add.s $f2, $f2, $f7 #-b + sqrt(Δ) #solution 1 117 | div.s $f0, $f2, $f1 # ( -b + sqrt(Δ) )/ 2a # 118 | 119 | la $a0, msg1 #print str 120 | li $v0, 4 121 | syscall 122 | 123 | mov.s $f12, $f0 #print float 124 | li $v0, 2 125 | syscall 126 | 127 | sub.s $f2, $f2, $f7 # -b - sqrt(Δ) # 128 | div.s $f0, $f2, $f1 # ( -b - sqrt(Δ) )/ 2a #solution 2 129 | 130 | la $a0,endl # print end of line 131 | li $v0, 4 132 | syscall 133 | 134 | la $a0, msg2 #print str 135 | li $v0, 4 136 | syscall 137 | 138 | mov.s $f12, $f0 #print float 139 | li $v0, 2 140 | syscall 141 | j Exit 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | # non of previous conditions/cases exit 150 | if_non_case_: #........... 151 | 152 | 153 | 154 | 155 | Exit: li $v0, 10 156 | syscall #au revoir... 157 | -------------------------------------------------------------------------------- /previous-exams/practice/Binary.s: -------------------------------------------------------------------------------- 1 | .data 2 | prompt:.asciiz "Enter an integer:\n " 3 | addsh: .asciiz " The sum in decimal is: \n" 4 | binar: .asciiz " The sum in binary is:\n " 5 | sgt: .asciiz " The second integer is greater than the first. n\n" 6 | fgt: .asciiz " The first integer is greater than the second.\n" 7 | equal: .asciiz "The two entered values are equal: \n" 8 | 9 | .text 10 | .globl __start 11 | main: 12 | 13 | li $v0, 4 #print string code 14 | la $a0, prompt #argument 15 | syscall #execute, service print stri 16 | 17 | li $v0, 5 # read integer 18 | syscall #v0 gets the returned value 19 | move $s0, $v0 #set v0 -> s0 20 | 21 | li $v0, 4 22 | la $a0, prompt 23 | syscall 24 | 25 | li $v0, 5 26 | syscall 27 | move $s1, $v0 #set v0 -> s1 28 | add $s2, $s1, $s0 #s2 -> s0 + s1 29 | 30 | li $v0, 4 #print string 31 | la $a0, add #argument 32 | syscall 33 | 34 | move $a0, $s2 35 | li $v0, 1 #print int 36 | syscall 37 | 38 | # Output "sum in binary is:". 39 | la $a0, bin 40 | li $v0, 4 41 | syscall 42 | 43 | 44 | # Output the binary number. (This is done by isolating one bit 45 | # at a time, adding it to the ASCII code for '0', and outputting 46 | # the character. It is important that the bits are output in 47 | # most-to-least significant bit order. 48 | move $t2, $a0 49 | li $s1, 32 # Set up a loop counter 50 | Loop: 51 | rol $t2, $t2, 1 # Roll the bits left by one bit - wraps highest bit to lowest bit. 52 | and $t0, $t2, 1 # Mask off low bit (logical AND with 000...0001) 53 | add $t0, $t0, 48 # Combine it with ASCII code for '0', becomes 0 or 1 54 | 55 | move $a0, $t0 # Output the ASCII character 56 | li $v0, 11 57 | syscall 58 | 59 | subi $s1, $s1, 1 # Decrement loop counter 60 | bne $s1, $zero, Loop # Keep looping if loop counter is not zero 61 | 62 | slt $t0, $s0, $s1 63 | beq $t0, $0, else 64 | li $v0, 4 65 | la $a0, sgt 66 | syscall 67 | j jump 68 | else: 69 | li $v0, 4 70 | la $a0, fgt 71 | syscall 72 | 73 | jump: 74 | li $v0, 10 #system call for exit 75 | syscall -------------------------------------------------------------------------------- /previous-exams/practice/FloatToSumOf1n0.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg0: .asciiz "Total number of 0 = " 10 | msg1: .asciiz "Total number of 1 = " 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | loop: 22 | li $v0, 6 #read float 23 | syscall 24 | mov.s $f1, $f0 25 | 26 | mov.s $f12, $f1 #print float 27 | li $v0, 2 28 | syscall 29 | jal print_endl 30 | 31 | c.eq.s $f1, $f7 #if n = 0 exit case 32 | bc1t Exit 33 | 34 | mfc1 $s0, $f1 #move fro float register to int register 35 | li $t0, 0 #how many 0 36 | li $t1, 0 #how many 1 37 | li $t2, 0 #counter 38 | li $t5, 32 39 | loop2: addi $t2, $t2, 1 #counter ++ 40 | andi $t3, $s0, 1 #check if the last digit is 1 41 | 42 | if: bne $t3, $zero, else 43 | addi $t0, $t0, 1 44 | j end_if 45 | else: 46 | addi $t1, $t1, 1 47 | end_if: 48 | 49 | srl $s0, $s0, 1 50 | 51 | beq $t2, $t5, exit_loop2 #when 32 loops 52 | j loop2 53 | exit_loop2: 54 | 55 | li $v0, 4 56 | la $a0, msg0 57 | syscall 58 | move $a0, $t0 # print how many 0 59 | li $v0, 1 60 | syscall 61 | jal print_endl 62 | 63 | li $v0, 4 64 | la $a0, msg1 65 | syscall 66 | move $a0, $t1 # print how many 1 67 | li $v0, 1 68 | syscall 69 | jal print_endl 70 | 71 | j loop 72 | 73 | 74 | Exit: li $v0, 10 75 | syscall #au revoir... 76 | 77 | 78 | print_endl: la $a0,endl # system call to print 79 | li $v0, 4 # out a newline 80 | syscall 81 | jr $ra -------------------------------------------------------------------------------- /previous-exams/practice/PowerofaNumber.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # text segment # 4 | # # 5 | ################################################# 6 | .text 7 | .globl __start 8 | __start: 9 | #-------------start of main program-------------# 10 | jal read_int #base 11 | move $s0, $v0 12 | 13 | move $a0, $s0 14 | jal print_int 15 | 16 | jal print_endl 17 | 18 | jal read_int #power 19 | move $s1, $v0 20 | 21 | move $a0,$s1 22 | jal print_int 23 | 24 | li $t0,1 25 | li $s3,1 26 | pow: mul $s3,$s3,$s0 27 | addi $t0,$t0,1 28 | bgt $t0,$s1, print_results 29 | j pow 30 | 31 | print_results: jal print_endl 32 | move $a0,$s3 33 | jal print_int 34 | 35 | exit: li $v0, 10 36 | syscall #au revoir... 37 | #-------------end of main program--------------# 38 | 39 | #-------------start of procedures--------------# 40 | print_endl: la $a0,endl 41 | li $v0, 4 #system call to print 42 | syscall 43 | jr $ra 44 | 45 | read_int: li $v0, 5 #$v0 <--- integer 46 | syscall 47 | jr $ra 48 | 49 | print_int: #move $a0, ??? #??? --> register with integer 50 | li $v0, 1 51 | syscall 52 | jr $ra 53 | #-------------end of procedures---------------# 54 | 55 | ################################################# 56 | # # 57 | # data segment # 58 | # # 59 | ################################################# 60 | 61 | .data 62 | endl: .asciiz "\n" -------------------------------------------------------------------------------- /previous-exams/practice/SameLetterStrings.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str1: .space 50 10 | str2: .space 50 11 | message: .asciiz "Same letters:\n" 12 | space: .asciiz " in postition " 13 | ################################################# 14 | # # 15 | # text segment # 16 | # # 17 | ################################################# 18 | 19 | .text 20 | .globl __start 21 | #read first int and move it to a temp reg 22 | __start: 23 | 24 | li $v0, 8 # code to read a string 25 | la $a0, str1 # ??? --> label with string 26 | li $a1, 50 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 27 | syscall 28 | li $v0, 4 29 | la $a0, str1 # ??? --> label with string 30 | syscall 31 | 32 | li $v0, 8 # code to read a string 33 | la $a0, str2 # ??? --> label with string 34 | li $a1, 50 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 35 | syscall 36 | li $v0, 4 37 | la $a0, str2 # ??? --> label with string 38 | syscall 39 | 40 | li $v0, 4 41 | la $a0, message # ??? --> label with string 42 | syscall 43 | #----------------------------------------------------------------------------------------- 44 | la $t0, str1 45 | la $t1, str2 46 | li $t8, 10 47 | li $t9, 1 48 | loop: lbu $t2, 0($t0) 49 | lbu $t3, 0($t1) 50 | bne $t2, $t3, different_letter 51 | beq $t2, $t8, Exit 52 | move $a0, $t2 53 | jal print_char 54 | li $v0, 4 55 | la $a0, space # ??? --> label with string 56 | syscall 57 | 58 | move $a0, $t9 # ??? --> register with integer 59 | li $v0, 1 60 | syscall 61 | la $a0,endl # system call to print 62 | li $v0, 4 # out a newline 63 | syscall 64 | 65 | different_letter: 66 | addi $t9, $t9, 1 67 | addi $t0, $t0, 1 68 | addi $t1, $t1, 1 69 | lbu $t4, 0($t0) 70 | lbu $t5, 0($t1) 71 | beq $t4, $zero, Exit 72 | beq $t5, $zero, Exit 73 | j loop 74 | 75 | 76 | 77 | Exit: li $v0, 10 78 | syscall #au revoir... 79 | 80 | 81 | 82 | print_char: li $v0, 11 83 | syscall 84 | jr $ra -------------------------------------------------------------------------------- /previous-exams/practice/ScientificNotation.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | pos_inf: .word 0x7f800000 10 | neg_inf: .word 0xff800000 11 | nan: .word 0x7fffffff 12 | prompt: .asciiz "Give real number:\n" 13 | output_msg: .asciiz "Number in scientific notation is:\n" 14 | minus: .asciiz "-" 15 | plus: .asciiz "+" 16 | one_c: .asciiz "1." 17 | times: .asciiz " x " 18 | power: .asciiz "2^" 19 | bin_one: .asciiz "1" 20 | bin_zero: .asciiz "0" 21 | 22 | ################################################# 23 | # # 24 | # text segment # 25 | # # 26 | ################################################# 27 | 28 | .text 29 | .globl __start 30 | 31 | __start: 32 | 33 | while: 34 | li $v0, 4 35 | la $a0, prompt 36 | syscall # prompt for input 37 | 38 | li $v0, 6 39 | syscall # get input 40 | 41 | mov.s $f12, $f0 42 | li $v0, 2 43 | syscall # display input 44 | 45 | la $a0, endl # system call to print 46 | li $v0, 4 # out a newline 47 | syscall 48 | 49 | li.s $f1, 0.0 50 | c.eq.s $f0, $f1 #if $f0 == $f1 cc is true(1) else is false(0) 51 | bc1t Exit 52 | 53 | li $v0, 4 54 | la $a0, output_msg 55 | syscall # display output message 56 | 57 | mfc1 $t0, $f0 58 | srl $t1, $t0, 31 59 | 60 | beqz $t1, plus_sign 61 | la $a0, minus 62 | j continue0 63 | plus_sign: 64 | la $a0, plus 65 | continue0: 66 | 67 | li $v0, 4 68 | syscall # print sign 69 | 70 | la $a0, one_c 71 | syscall # print 1. 72 | 73 | li $t1, 0x7fffff 74 | and $t1, $t1, $t0 #masking 75 | 76 | li $t3, 0 77 | li $t4, 23 78 | sec_while: 79 | andi $t2, $t1, 1 80 | bnez $t2, exit_sec_while 81 | srl $t1, $t1, 1 82 | addi $t3, $t3, 1 83 | beq $t3, $t4, exit_sec_while 84 | j sec_while 85 | 86 | exit_sec_while: 87 | 88 | bne $t3, $t4, continue1 89 | move $a0, $zero 90 | li $v0, 1 91 | syscall 92 | j exponent 93 | continue1: 94 | 95 | sub $t4, $t4, $t3 96 | move $t5, $t4 97 | addi $t5, $t5, -1 98 | li $t6, 1 99 | sec_while1: 100 | beqz $t5, exit_sec_while1 101 | addi $t5, $t5, -1 102 | sll $t6, $t6, 1 103 | j sec_while1 104 | 105 | exit_sec_while1: 106 | 107 | sec_while2: 108 | beq $t4, $zero, exit_sec_while2 109 | and $t5, $t1, $t6 110 | bne $t5, $zero, one 111 | la $a0, bin_zero 112 | j printing 113 | one: 114 | la $a0, bin_one 115 | printing: 116 | li $v0, 4 117 | syscall 118 | sll $t1, $t1, 1 119 | addi $t4, $t4, -1 120 | j sec_while2 121 | 122 | exit_sec_while2: 123 | 124 | exponent: 125 | 126 | li $v0, 4 127 | la $a0, times 128 | syscall 129 | 130 | li $v0, 4 131 | la $a0, power 132 | syscall 133 | 134 | li $t1, 0x7f800000 135 | and $t1, $t0, $t1 136 | srl $t1, $t1, 23 137 | addi $t1, $t1, -127 138 | 139 | move $a0, $t1 140 | li $v0, 1 141 | syscall 142 | 143 | la $a0, endl # system call to print 144 | li $v0, 4 # out a newline 145 | syscall 146 | 147 | j while 148 | 149 | Exit: 150 | li $v0, 10 151 | syscall # au revoir... 152 | 153 | -------------------------------------------------------------------------------- /previous-exams/practice/StringFourNumber.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str: .space 21 10 | msg1: .asciiz "\nk = " 11 | msg2: .asciiz "\nm = " 12 | startmsg: .asciiz "Give me the string:" 13 | bye: .asciiz "bye:)" 14 | ################################################# 15 | # # 16 | # text segment # 17 | # # 18 | ################################################# 19 | 20 | .text 21 | .globl __start 22 | #read first int and move it to a temp reg 23 | __start: 24 | 25 | loop: 26 | la $a0, startmsg 27 | li $v0, 4 28 | syscall 29 | li $v0, 8 30 | la $a0, str 31 | li $a1, 21 # ??? --> label with string 32 | syscall 33 | move $t0, $a0 #t0 is the string 34 | la $a0, str 35 | li $v0, 4 36 | syscall 37 | 38 | 39 | li $v0, 4 40 | la $a0, msg1 # ??? --> label with string 41 | syscall 42 | li $v0, 5 # $v0 <--- integer 43 | syscall 44 | move $t1, $v0 #t1 = n 45 | move $a0, $t1 # ??? --> register with integer 46 | li $v0, 1 47 | syscall 48 | 49 | 50 | 51 | li $v0, 4 52 | la $a0, msg2 # ??? --> label with string 53 | syscall 54 | li $v0, 5 # $v0 <--- integer 55 | syscall 56 | move $t2, $v0 #t2 = m 57 | move $a0, $t2 # ??? --> register with integer 58 | li $v0, 1 59 | syscall 60 | la $a0,endl # system call to print 61 | li $v0, 4 # out a newline 62 | syscall 63 | 64 | add $k1, $t1, $t2 65 | beq $k1, $zero, Exit 66 | 67 | addi $t1, $t1, -1 68 | li $t9, 4 69 | mult $t1, $t9 70 | mflo $t3 71 | 72 | addi $t2, $t2, -1 73 | li $t9, 4 74 | mult $t2, $t9 75 | mflo $t4 76 | #------------------------------------------------------------------------------------ 77 | 78 | la $s0, str 79 | 80 | li $s2, 0 81 | li $s5, 20 82 | loop2: beq $s2, $t3, changeN 83 | beq $s2, $t4, changeM 84 | c1: 85 | lbu $s3, 0($s0) 86 | li $v0, 11 87 | move $a0, $s3 88 | syscall 89 | addi $s0, $s0, 1 90 | addi $s2, $s2, 1 91 | bge $s2, $s5, exit1 92 | j loop2 93 | exit1: 94 | la $a0,endl # system call to print 95 | li $v0, 4 # out a newline 96 | syscall 97 | j loop 98 | Exit: la $a0, bye 99 | li $v0, 4 100 | syscall 101 | li $v0, 10 102 | syscall #au revoir... 103 | 104 | 105 | changeN: la $s1, str 106 | add $s1, $s1, $t4 107 | lbu $s3, 0($s1) 108 | li $v0, 11 109 | move $a0, $s3 110 | syscall 111 | addi $s1, $s1, 1 112 | lbu $s3, 0($s1) 113 | li $v0, 11 114 | move $a0, $s3 115 | syscall 116 | addi $s1, $s1, 1 117 | lbu $s3, 0($s1) 118 | li $v0, 11 119 | move $a0, $s3 120 | syscall 121 | addi $s1, $s1, 1 122 | lbu $s3, 0($s1) 123 | li $v0, 11 124 | move $a0, $s3 125 | syscall 126 | addi $s0, $s0, 4 127 | addi $s2, $s2, 4 128 | j c1 129 | 130 | changeM: la $s1, str 131 | add $s1, $s1, $t3 132 | lbu $s3, 0($s1) 133 | li $v0, 11 134 | move $a0, $s3 135 | syscall 136 | addi $s1, $s1, 1 137 | lbu $s3, 0($s1) 138 | li $v0, 11 139 | move $a0, $s3 140 | syscall 141 | addi $s1, $s1, 1 142 | lbu $s3, 0($s1) 143 | li $v0, 11 144 | move $a0, $s3 145 | syscall 146 | addi $s1, $s1, 1 147 | lbu $s3, 0($s1) 148 | li $v0, 11 149 | move $a0, $s3 150 | syscall 151 | addi $s0, $s0, 4 152 | addi $s2, $s2, 4 153 | j c1 154 | 155 | 156 | -------------------------------------------------------------------------------- /previous-exams/practice/StringInterleaving.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | m: .asciiz "0123456789" 10 | n: .asciiz "abcdefghij" 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | 22 | la $s0, m 23 | la $s1, n 24 | 25 | li $t0, 0 26 | li $t1, 10 27 | 28 | loop: lbu $t2, 0($s0) #get 1st,2nd, ... character of m 29 | li $v0, 11 30 | move $a0, $t2 31 | syscall 32 | addi $s0, $s0, 1 33 | 34 | lbu $t3, 0($s1) #get 1st,2nd, ... character of n 35 | li $v0, 11 36 | move $a0, $t3 37 | syscall 38 | addi $s1, $s1, 1 39 | 40 | addi $t0, $t0, 1 41 | 42 | beq $t0, $t1, Exit 43 | 44 | j loop 45 | 46 | 47 | Exit: li $v0, 10 48 | syscall #au revoir... -------------------------------------------------------------------------------- /previous-exams/practice/StringPyramid.s: -------------------------------------------------------------------------------- 1 | #Print the entered string as a pyramid 2 | ################################################# 3 | # # 4 | # text segment # 5 | # # 6 | ################################################# 7 | .text 8 | .globl __start 9 | __start: 10 | #-------------start of main program-------------# 11 | print_string : la $a0, message 12 | li $v0 , 4 13 | syscall 14 | 15 | read_string : li $v0,8 16 | la $a0 , string 17 | li $a1, 26 18 | syscall 19 | 20 | li $s0, 6 21 | la $s1, string 22 | 23 | print_string_before: la $a0,string 24 | li $v0,4 25 | syscall 26 | 27 | pyramid: 28 | addi $s0, $s0,-1 29 | move $t0,$s0 #t0->number of spaces 30 | 31 | li $t3, 2 32 | mul $t4, $t0,$t3 33 | li $t5, 11 34 | sub $t3, $t5, $t4 #t3->number of char in the line 35 | 36 | jal print_endl 37 | 38 | check: li $s3, 3 #last line case/only print chars 39 | beq $t0, $zero,loop_char 40 | 41 | li $s3, 0 #flag 42 | loop_space : 43 | print_space: la $a0, space 44 | li $v0, 4 45 | syscall 46 | 47 | addi $t0,$t0,-1 48 | bne $t0, $zero, loop_space 49 | addi $s3, $s3, 1 #flag 50 | li $t6 , 3 51 | beq $s3,$t6, pyramid 52 | 53 | loop_char: 54 | print_char: lbu $a0, 0($s1) 55 | addi $s1, $s1, 1 56 | li $v0 , 11 57 | syscall 58 | 59 | addi $t3,$t3,-1 60 | bne $t3, $zero, loop_char 61 | 62 | li $t6, 3 63 | beq $s3, $t6, exit 64 | addi $s3, $s3, 1 #flag 65 | li $t6, 2 66 | beq $s3, $t2, loop_space 67 | 68 | 69 | beq $t0,$zero, pyramid 70 | 71 | 72 | exit: li $v0, 10 73 | syscall #au revoir... 74 | #-------------end of main program--------------# 75 | 76 | #-------------start of procedures--------------# 77 | print_endl: la $a0,endl 78 | li $v0, 4 #system call to print 79 | syscall 80 | jr $ra 81 | 82 | #-------------end of procedures---------------# 83 | 84 | ################################################# 85 | # # 86 | # data segment # 87 | # # 88 | ################################################# 89 | 90 | .data 91 | message: .asciiz "Give a string of 25 chars: " 92 | endl: .asciiz "\n" 93 | string : .space 26 94 | space : .asciiz " " -------------------------------------------------------------------------------- /previous-exams/practice/SumVectrorsFloat.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | vector1: .float 1.0,2.0,3.0, 4.0, 5.0, 9.0 10 | vector2: .float 1.0,1.0,2.0, 1.0, 3.0, 1.0 11 | n: .word 6 #number of elements in the vectors 12 | vector3: .space 40 13 | parenthesi1: .asciiz "[ " 14 | parenthesi2: .asciiz " ]" 15 | sin: .asciiz " + " 16 | space: .asciiz " " 17 | equal: .asciiz " = " 18 | ################################################# 19 | # # 20 | # text segment # 21 | # # 22 | ################################################# 23 | 24 | .text 25 | .globl __start 26 | #read first int and move it to a temp reg 27 | __start: 28 | lbu $t0, n 29 | 30 | la $a0, parenthesi1 31 | jal print_str 32 | la $t1, vector1 #address 33 | loop1: 34 | l.s $f0, 0($t1) 35 | mov.s $f12, $f0 36 | li $v0, 2 #print float 37 | syscall 38 | addi $t1, $t1, 4 #next element in vector 39 | addi $t2, $t2, 1 40 | beq $t2, $t0, exit1 #if counter equals to number of elements in vector 41 | la $a0, space #print space 42 | jal print_str 43 | j loop1 44 | exit1: la $a0, parenthesi2 45 | jal print_str 46 | #--------------------------------------------------- 47 | la $a0, sin 48 | jal print_str 49 | #--------------------------------------------------- 50 | la $a0, parenthesi1 51 | jal print_str 52 | la $t3, vector2 #address 53 | li $t2, 0 54 | loop2: 55 | l.s $f0, 0($t3) 56 | mov.s $f12, $f0 57 | li $v0, 2 #print float 58 | syscall 59 | addi $t3, $t3, 4 #next element in vector 60 | addi $t2, $t2, 1 61 | beq $t2, $t0, exit2 #if counter equals to number of elements in vector 62 | la $a0, space #print space 63 | jal print_str 64 | j loop2 65 | exit2: la $a0, parenthesi2 66 | jal print_str 67 | #--------------------------------------------------- 68 | la $a0, equal 69 | jal print_str 70 | #--------------------------------------------------- 71 | la $a0, parenthesi1 72 | jal print_str 73 | la $t1, vector1 74 | li $t2, 0 75 | la $t3, vector2 76 | 77 | loop3: l.s $f0, 0($t1) 78 | l.s $f1, 0($t3) 79 | add.s $f3, $f0, $f1 80 | mov.s $f12, $f3 81 | li $v0, 2 #print float 82 | syscall 83 | addi $t1, $t1, 4 #next element in vector1 84 | addi $t3, $t3, 4 #next element in vector2 85 | addi $t2, $t2, 1 86 | beq $t2, $t0, exit3 #if counter equals to number of elements in vector 87 | la $a0, space #print space 88 | jal print_str 89 | j loop3 90 | exit3: la $a0, parenthesi2 91 | jal print_str 92 | 93 | Exit: li $v0, 10 94 | syscall #au revoir... 95 | 96 | 97 | 98 | 99 | print_str: li $v0, 4 100 | syscall 101 | jr $ra -------------------------------------------------------------------------------- /previous-exams/practice/sdi2100046-achlab.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg1: .asciiz "Give me the number a:\n" 10 | msg2: .asciiz "Give me the number b:\n" 11 | msg3: .asciiz "Give me the number c:\n" 12 | msgNULL: .asciiz "Root negative, no solutions\n" 13 | minusfour: .float -4.0 14 | 15 | ################################################# 16 | # # 17 | # text segment # 18 | # # 19 | ################################################# 20 | 21 | .text 22 | .globl __start 23 | #read first int and move it to a temp reg 24 | __start: 25 | li $v0, 6 # $f0 <--- float 26 | syscall 27 | mov.s $f1, $f0 28 | 29 | 30 | li $v0, 6 # $f0 <--- float 31 | syscall 32 | mov.s $f2, $f0 33 | 34 | li $v0, 6 # $f0 <--- float 35 | syscall 36 | mov.s $f3, $f0 37 | #--------------------------------------------------------------------------------------------- 38 | mul.s $f7, $f2, $f2 #b^2 39 | 40 | li.s $f5 , -4.0 41 | mul.s $f6, $f5, $f1 #-4a 42 | mul.s $f6, $f6, $f3 #-4ac 43 | 44 | add.s $f4, $f7, $f6 #f4=Δ , b^2-4av 45 | 46 | #/--------------------------------------------------------------------------------------------- 47 | 48 | switch_case_with_break: li.s $f8, 0.0 49 | li.s $f9, -1.0 50 | case001: c.eq.s $f4, $f8 # this condition/case is not valid, check next case 51 | bc1f case002 52 | if_case001: 53 | mul.s $f10, $f2, $f9 #-b 54 | add.s $f11, $f1, $f1 #2a 55 | div.s $f10, $f10, $f11 56 | 57 | mov.s $f12, $f10 # ??? --> FP register with float 58 | li $v0, 2 59 | syscall 60 | 61 | j Exit 62 | case002: c.lt.s $f4, $f8 # this condition/case is not valid, check next case 63 | bc1f case00n 64 | if_case002: 65 | li $v0, 4 66 | la $a0, msgNULL # ??? --> label with string 67 | syscall 68 | 69 | j Exit 70 | 71 | #........... 72 | 73 | case00n: sqrt.s $f4, $f4 #sqr(Δ) 74 | mul.s $f10, $f2, $f9 #-b 75 | add.s $f11, $f1, $f1 #2a 76 | 77 | add.s $f12, $f4, $f10 78 | div.s $f12, $f12, $f11 79 | li $v0, 2 80 | syscall 81 | 82 | la $a0,endl # system call to print 83 | li $v0, 4 # out a newline 84 | syscall 85 | 86 | sub.s $f12, $f10, $f4 87 | div.s $f12, $f12, $f11 88 | li $v0, 2 89 | syscall 90 | 91 | j Exit 92 | 93 | 94 | Exit: li $v0, 10 95 | syscall #au revoir... 96 | -------------------------------------------------------------------------------- /previous-exams/practice/sim1.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg: .asciiz "POWER-SUM=" 10 | epi: .asciiz "*10^" 11 | plus: .asciiz " + " 12 | ################################################# 13 | # # 14 | # text segment # 15 | # # 16 | ################################################# 17 | 18 | .text 19 | .globl __start 20 | #read first int and move it to a temp reg 21 | __start: 22 | 23 | start: 24 | li $t0, 1 25 | li $t1, 0 #count of digits 26 | li $t2, 10 27 | li$v0, 5 # $v0 <--- integer 28 | li $k0, 0 29 | syscall 30 | move $t7, $v0 31 | beq $t7, $zero, Exit 32 | count_of_digits: 33 | mult $t0, $t2 34 | mflo $t0 35 | addi $t1, 1 36 | blt $t0, $t7, count_of_digits 37 | 38 | 39 | li $v0, 4 40 | la $a0, msg # ??? --> label with string 41 | syscall 42 | 43 | 44 | loop: 45 | div $t0, $t0, $t2 46 | div $t5, $t7, $t0 47 | mfhi $t7 48 | 49 | move $a0, $t5 # ??? --> register with integer 50 | li $v0, 1 51 | syscall 52 | 53 | li $v0, 4 54 | la $a0, epi # ??? --> label with string 55 | syscall 56 | 57 | addi $t1, $t1, -1 58 | move $a0, $t4 # ??? --> register with integer 59 | li $v0, 1 60 | syscall 61 | 62 | if: beq $t1, $k0, target 63 | li $v0, 4 64 | la $a0, plus # ??? --> label with string 65 | syscall 66 | target: 67 | 68 | bgtz $t1, loop 69 | 70 | la $a0,endl # system call to print 71 | li $v0, 4 # out a newline 72 | syscall 73 | 74 | j start 75 | 76 | 77 | 78 | 79 | Exit: li $v0, 10 80 | syscall #au revoir... 81 | -------------------------------------------------------------------------------- /previous-exams/practice/sim2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | str: .space 5 10 | numbers: .asciiz "01234567" 11 | zero: .asciiz "000" 12 | one: .asciiz "001" 13 | two: .asciiz "010" 14 | three: .asciiz "011" 15 | four: .asciiz "100" 16 | five: .asciiz "101" 17 | six: .asciiz "110" 18 | seven: .asciiz "111" 19 | error: .asciiz "Wrong input only octadecimal\n" 20 | bye: .asciiz "\nbye:)\n" 21 | ################################################# 22 | # # 23 | # text segment # 24 | # # 25 | ################################################# 26 | 27 | .text 28 | .globl __start 29 | #read first int and move it to a temp reg 30 | __start: 31 | li $s4, 4 32 | loop: 33 | 34 | li $v0, 8 # code to read a string 35 | la $a0, str # ??? --> label with string 36 | li $a1, 5 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 37 | syscall 38 | 39 | li $k0, 0 40 | 41 | 42 | la $s0, str 43 | lbu $t0, 0($s0) 44 | lbu $t1, 1($s0) 45 | lbu $t2, 2($s0) 46 | lbu $t3, 3($s0) 47 | 48 | move $a0, $t0 49 | jal switch_case_with_break 50 | 51 | move $a0, $t1 52 | jal switch_case_with_break 53 | 54 | move $a0, $t2 55 | jal switch_case_with_break 56 | 57 | move $a0, $t3 58 | jal switch_case_with_break 59 | 60 | beq $k0, $s4, Exit 61 | 62 | li $v0, 4 63 | la $a0, endl # ??? --> label with string 64 | syscall 65 | 66 | j loop 67 | 68 | 69 | 70 | Exit: li $v0, 4 71 | la $a0, bye # ??? --> label with string 72 | syscall 73 | li $v0, 10 74 | syscall #au revoir... 75 | 76 | 77 | switch_case_with_break: move $t8, $a0 78 | case001: li $t9, 48 79 | bne $a0, $t9 , case002 # this condition/case is not valid, check next case 80 | if_case001: li $v0, 4 81 | la $a0, zero # ??? --> label with string 82 | syscall 83 | addi $k0, 1 84 | jr $ra 85 | 86 | case002: li $t9, 49 87 | bne $a0, $t9 , case003 # this condition/case is not valid, check next case 88 | if_case002: li $v0, 4 89 | la $a0, one # ??? --> label with string 90 | syscall 91 | jr $ra 92 | 93 | case003: li $t9, 50 94 | bne $a0, $t9 , case004 # this condition/case is not valid, check next case 95 | if_case003: li $v0, 4 96 | la $a0, two # ??? --> label with string 97 | syscall 98 | jr $ra 99 | 100 | case004: li $t9, 51 101 | bne $a0, $t9 , case005 # this condition/case is not valid, check next case 102 | if_case004: li $v0, 4 103 | la $a0, three # ??? --> label with string 104 | syscall 105 | jr $ra 106 | 107 | case005: li $t9, 52 108 | bne $a0, $t9 , case006 # this condition/case is not valid, check next case 109 | if_case005: li $v0, 4 110 | la $a0, four # ??? --> label with string 111 | syscall 112 | jr $ra 113 | 114 | case006: li $t9, 53 115 | bne $a0, $t9 , case007 # this condition/case is not valid, check next case 116 | if_case006: li $v0, 4 117 | la $a0, five # ??? --> label with string 118 | syscall 119 | jr $ra 120 | 121 | case007: li $t9, 54 122 | bne $a0, $t9 , case008 # this condition/case is not valid, check next case 123 | if_case007: li $v0, 4 124 | la $a0, six # ??? --> label with string 125 | syscall 126 | jr $ra 127 | 128 | case008: li $t9, 55 129 | bne $a0, $t9 , else_case_ # this condition/case is not valid, check next case 130 | if_case008: li $v0, 4 131 | la $a0, seven # ??? --> label with string 132 | syscall 133 | jr $ra 134 | 135 | else_case_: li $t9, 0 136 | bne $a0, $t9 , if_non_case 137 | li $v0, 4 138 | la $a0, endl # ??? --> label with string 139 | syscall 140 | jr $ra 141 | 142 | if_non_case: li $v0, 4 143 | la $a0, error # ??? --> label with string 144 | syscall # non of previous conditions/cases exit 145 | j loop 146 | -------------------------------------------------------------------------------- /previous-exams/practice/sim3.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msgA: .asciiz "FOUND-INTEGER= " 10 | msgB: .asciiz "NOT-INTEGER= " 11 | msg: .asciiz "0 is the exit bye :)\n" 12 | ################################################# 13 | # # 14 | # text segment # 15 | # # 16 | ################################################# 17 | 18 | .text 19 | .globl __start 20 | #read first int and move it to a temp reg 21 | __start: 22 | 23 | loop: 24 | li $v0, 6 #read float # $f0 <--- float 25 | syscall 26 | mov.s $f1, $f0 27 | 28 | c.eq.s $f1, $f5 #checking if 0 29 | bc1t Exit 30 | 31 | cvt.w.s $f2, $f1 32 | cvt.s.w $f2, $f2 33 | 34 | c.eq.s $f1, $f2 35 | bc1t msg1 36 | bc1f msg2 37 | 38 | msg1: li $v0, 4 39 | la $a0, msgA # ??? --> label with string 40 | syscall 41 | 42 | mov.s $f12, $f1 # ??? --> FP register with float 43 | li $v0, 2 44 | syscall 45 | 46 | la $a0,endl # system call to print 47 | li $v0, 4 # out a newline 48 | syscall 49 | 50 | j loop 51 | 52 | msg2: li $v0, 4 53 | la $a0, msgB # ??? --> label with string 54 | syscall 55 | 56 | mov.s $f12, $f1 # ??? --> FP register with float 57 | li $v0, 2 58 | syscall 59 | 60 | la $a0,endl # system call to print 61 | li $v0, 4 # out a newline 62 | syscall 63 | j loop 64 | 65 | 66 | Exit: li $v0, 4 67 | la $a0, msg # ??? --> label with string 68 | syscall 69 | li $v0, 10 70 | syscall #au revoir... 71 | -------------------------------------------------------------------------------- /previous-exams/simulation-exam-2022/README.md: -------------------------------------------------------------------------------- 1 | # simulation exam 2 | the simulatoin exam -------------------------------------------------------------------------------- /previous-exams/simulation-exam-2022/ekfwnisi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tonykalantzis/assembly-MIPS-code-examples/e15544c23aeb6dbe9dbb7b25a7b6dae7687f6213/previous-exams/simulation-exam-2022/ekfwnisi.jpg -------------------------------------------------------------------------------- /previous-exams/simulation-exam-2022/exam.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg1: .asciiz "POWERS-SUM=" 10 | epi: .asciiz "*10^" 11 | sin: .asciiz " + " 12 | ################################################# 13 | # # 14 | # text segment # 15 | # # 16 | ################################################# 17 | 18 | .text 19 | .globl __start 20 | #read first int and move it to a temp reg 21 | __start: 22 | 23 | 24 | read_int: li $t9, 0 #n of digits 25 | li $t8, 10 # t8 divisor 26 | li $k0, 10 27 | li $k1, 1 28 | li $s1, -1 29 | 30 | li $v0, 5 # $v0 <--- integer 31 | syscall 32 | move $t0 , $v0 33 | beq $t0, $zero, exit 34 | 35 | li $v0, 4 #print msg 36 | la $a0, msg1 # ??? --> label with string 37 | syscall 38 | 39 | loop_count_digits: addi $t9, 1 #HEREE 40 | mult $t8, $k0 41 | mflo $t8 42 | blt $t8, $t0 , loop_count_digits 43 | 44 | div $t8, $k0 45 | mflo $t8 # 1 digit 46 | move $t4, $t0 # remainder 47 | 48 | loop2: div $t4, $t8 49 | mflo $t3 50 | mfhi $t4 51 | jal print_digit 52 | div $t8, $k0 # /10 53 | mflo $t8 54 | sub $t9 , $t9 , $k1 # -1 digit 55 | bgt $t9, $s1, loop2 56 | 57 | la $a0,endl # system call to print 58 | li $v0, 4 # out a newline 59 | syscall 60 | 61 | j read_int 62 | 63 | 64 | 65 | exit: li $v0, 10 66 | syscall #au revoir... 67 | 68 | 69 | 70 | print_digit: move $a0, $t3 # ??? --> register with integer 71 | li $v0, 1 72 | syscall 73 | 74 | li $v0, 4 #print msg 75 | la $a0, epi # ??? --> label with string 76 | syscall 77 | 78 | move $a0, $t9 # ??? --> register with integer 79 | li $v0, 1 80 | syscall 81 | 82 | blt $t9, $k1, target # if $t0 == $t1 then target 83 | li $v0, 4 #print msg 84 | la $a0, sin # ??? --> label with string 85 | syscall 86 | target: 87 | 88 | jr $ra 89 | 90 | -------------------------------------------------------------------------------- /previous-exams/simulation-exam-2022/exam2.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | numbers: .asciiz "012345678" 10 | input: .asciiz "1234" 11 | zero: .asciiz "000" 12 | one: .asciiz "001" 13 | two: .asciiz "010" 14 | three: .asciiz "011" 15 | four: .asciiz "100" 16 | five: .asciiz "101" 17 | six: .asciiz "110" 18 | seven: .asciiz "111" 19 | check: .asciiz "0000" 20 | error: .asciiz "Wrong input\n" 21 | bye: .asciiz "bye :)\n" 22 | 23 | 24 | ################################################# 25 | # # 26 | # text segment # 27 | # # 28 | ################################################# 29 | 30 | .text 31 | .globl __start 32 | #read first int and move it to a temp reg 33 | __start: 34 | li $k0, 48 #ascii for 0 35 | li $s4, 4 36 | start: 37 | li $k1, 0 #counter of 0 38 | 39 | li $v0, 8 # code to read a string 40 | la $a0, input # ??? --> label with string 41 | li $a1, 5 # n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 42 | syscall 43 | 44 | la $s0, input 45 | lbu $t0, 0($s0) 46 | lbu $t1, 1($s0) 47 | lbu $t2, 2($s0) 48 | lbu $t3, 3($s0) 49 | 50 | move $a0, $t0 51 | jal switch_case_with_break 52 | 53 | 54 | move $a0, $t1 55 | jal switch_case_with_break 56 | 57 | 58 | move $a0, $t2 59 | jal switch_case_with_break 60 | 61 | 62 | move $a0, $t3 63 | jal switch_case_with_break 64 | 65 | la $a0,endl # system call to print 66 | li $v0, 4 # out a newline 67 | syscall 68 | 69 | beq $k1, $s4, Exit 70 | 71 | j start 72 | 73 | 74 | Exit: li $v0, 4 75 | la $a0,bye # ??? --> label with string 76 | syscall 77 | li $v0, 10 78 | syscall #au revoir... 79 | 80 | 81 | 82 | switch_case_with_break: move $t0, $a0 83 | case001: li $t8, 48 84 | bne $t0, $t8 , case002 85 | if_case001: li $v0, 4 86 | la $a0, zero # ??? --> label with string 87 | syscall 88 | addi $k1, 1 #counter of zeros 89 | jr $ra 90 | 91 | case002: li $t8, 49 92 | bne $t0, $t8 , case003 93 | if_case002: li $v0, 4 94 | la $a0, one # ??? --> label with string 95 | syscall 96 | jr $ra 97 | 98 | case003: li $t8, 50 99 | bne $t0, $t8 , case004 100 | if_case003: li $v0, 4 101 | la $a0, two # ??? --> label with string 102 | syscall 103 | jr $ra 104 | 105 | case004: li $t8, 51 106 | bne $t0, $t8 , case005 107 | if_case004: li $v0, 4 108 | la $a0,three # ??? --> label with string 109 | syscall 110 | jr $ra 111 | 112 | case005: li $t8, 52 113 | bne $t0, $t8 , case006 114 | if_case005: li $v0, 4 115 | la $a0,four # ??? --> label with string 116 | syscall 117 | jr $ra 118 | 119 | case006: li $t8, 53 120 | bne $t0, $t8 , case007 121 | if_case006: li $v0, 4 122 | la $a0,five # ??? --> label with string 123 | syscall 124 | jr $ra 125 | 126 | case007: li $t8, 54 127 | bne $t0, $t8 , case008 128 | if_case007: li $v0, 4 129 | la $a0,six # ??? --> label with string 130 | syscall 131 | jr $ra 132 | 133 | case008: li $t8, 55 134 | bne $t0, $t8, else_case 135 | if_case008: li $v0, 4 136 | la $a0, seven # ??? --> label with string 137 | syscall 138 | jr $ra 139 | 140 | else_case: li $t8, 0 141 | bne $t0, $t8 , if_non_case 142 | li $v0, 4 143 | la $a0, endl # ??? --> label with string 144 | syscall 145 | jr $ra 146 | 147 | 148 | if_non_case: li $v0, 4 149 | la $a0, error # ??? --> label with string 150 | syscall 151 | j start -------------------------------------------------------------------------------- /previous-exams/simulation-exam-2022/exam3.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | msg1: .asciiz "FOUND-INTEGER=" 10 | msg2: .asciiz "NOT-INTEGER=" 11 | ################################################# 12 | # # 13 | # text segment # 14 | # # 15 | ################################################# 16 | 17 | .text 18 | .globl __start 19 | #read first int and move it to a temp reg 20 | __start: 21 | 22 | start: 23 | 24 | 25 | read_float: li $v0, 6 # $f0 <--- float 26 | syscall 27 | mov.s $f1, $f0 28 | 29 | c.eq.s $f1, $f5 30 | bc1t Exit 31 | 32 | cvt.w.s $f2, $f1 33 | cvt.s.w $f2, $f2 34 | 35 | c.eq.s $f1, $f2 36 | bc1t print_msg1 37 | bc1f print_msg2 38 | 39 | print_msg1: li $v0, 4 40 | la $a0, msg1 # ??? --> label with string 41 | syscall 42 | 43 | mov.s $f12, $f2 # ??? --> FP register with float 44 | li $v0, 2 45 | syscall 46 | 47 | la $a0,endl # system call to print 48 | li $v0, 4 # out a newline 49 | syscall 50 | 51 | j start 52 | 53 | print_msg2: li $v0, 4 54 | la $a0, msg2 # ??? --> label with string 55 | syscall 56 | 57 | mov.s $f12, $f1 # ??? --> FP register with float 58 | li $v0, 2 59 | syscall 60 | 61 | la $a0,endl # system call to print 62 | li $v0, 4 # out a newline 63 | syscall 64 | 65 | j start 66 | 67 | 68 | 69 | Exit: li $v0, 10 70 | syscall #au revoir... 71 | -------------------------------------------------------------------------------- /useful-code/all_display.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | zero: .asciiz "Zero:" 10 | inf_m: .asciiz "Minus infinite:" 11 | inf_p: .asciiz "Plus infinite:" 12 | mess_nan: .asciiz "NaN:" 13 | mess_not_def: .asciiz "Not defined:" 14 | 15 | zer_f: .float 0.0 16 | m_inf: .word 0xff800000 17 | p_inf: .word 0x7f800000 18 | nan: .word 0x7fffffff 19 | not_defined: .word 0xffc00000 20 | x: .float 55.0 21 | y: .float -55.0 22 | 23 | ################################################# 24 | # # 25 | # text segment # 26 | # # 27 | ################################################# 28 | 29 | .text 30 | .globl __start 31 | __start: 32 | #-------------start of main program-------------# 33 | print_zero_mess: la $a0,zero 34 | jal print_string 35 | 36 | print_zero: l.s $f12,zer_f 37 | jal print_float 38 | jal printEndl 39 | 40 | print_m_inf_mess: la $a0,inf_m 41 | jal print_string 42 | 43 | print_m_inf: l.s $f12,m_inf 44 | jal print_float 45 | jal printEndl 46 | 47 | print_p_inf_mes: la $a0,inf_p 48 | jal print_string 49 | 50 | print_p_inf: l.s $f12,p_inf #load single 51 | jal print_float 52 | jal printEndl 53 | 54 | print_NaN_mess: la $a0,mess_nan 55 | jal print_string 56 | 57 | print_NaN: l.s $f12,nan 58 | jal print_float 59 | jal printEndl 60 | 61 | print_mess_not_def: la $a0,mess_not_def 62 | jal print_string 63 | 64 | print_not_def: l.s $f12, not_defined 65 | jal print_float 66 | jal printEndl 67 | 68 | 69 | 70 | Exit: li $v0, 10 71 | syscall #au revoir... 72 | #-------------end of main program--------------# 73 | 74 | #-------------start of procedures--------------# 75 | print_float: li $v0, 2 76 | syscall 77 | jr $ra 78 | 79 | printEndl: la $a0,endl 80 | li $v0, 4 81 | syscall 82 | jr $ra 83 | 84 | print_string: li $v0, 4 85 | syscall 86 | jr $ra 87 | 88 | #-------------end of procedures---------------# 89 | 90 | -------------------------------------------------------------------------------- /useful-code/basic_procedures.s: -------------------------------------------------------------------------------- 1 | ------------------------ STRINGS ------------------------- 2 | print_endl: la $a0,endl 3 | li $v0, 4 #system call to print 4 | syscall 5 | jr $ra 6 | 7 | read_string: li $v0, 8 #code to read a string 8 | la $a0, ??? #??? --> label with string 9 | li $a1, ?n? #n chars --> $a1=n+1 eg: (20+1)--> li $a1, 21 10 | syscall 11 | jr $ra 12 | 13 | print_string: li $v0, 4 14 | la $a0, ??? #??? --> label with string 15 | syscall 16 | jr $ra 17 | 18 | ----------------------- INTEGERS --------------------------- 19 | 20 | read_int: li $v0, 5 #$v0 <--- integer 21 | syscall 22 | jr $ra 23 | 24 | print_int: move $a0, ??? #??? --> register with integer 25 | li $v0, 1 26 | syscall 27 | jr $ra 28 | 29 | print_immidiate: li $v0, 1 30 | li $a0, ??? #??? --> immediate 31 | syscall 32 | jr $ra 33 | 34 | ------------------------ FLOATS ---------------------------- 35 | 36 | - SINGLE PRECISION 37 | 38 | read_float: li $v0, 6 # $f0 <--- float 39 | syscall 40 | jr $ra 41 | 42 | print_float_from_reg: mov.s $f12, ??? # ??? --> FP register with float 43 | li $v0, 2 44 | syscall 45 | jr $ra 46 | 47 | print_float_immidiate: li $v0, 2 48 | li.s $f12, ??? # ??? --> immediate, e.g -1.2345678 49 | syscall 50 | jr $ra 51 | 52 | 53 | - DOUBLE PRECISION 54 | 55 | read_double: li $v0, 7 # $f0 <--- double 56 | syscall 57 | jr $ra 58 | 59 | print_double_from_reg: mov.d $f12, ??? # ??? --> FP register with float 60 | li $v0, 3 61 | syscall 62 | jr $ra 63 | 64 | print_double_immidiate: li $v0, 3 65 | li.d $f12, ??? # ??? --> immediate, e.g -1.2345678 66 | syscall 67 | jr $ra 68 | 69 | - CONVERSIONS 70 | 71 | convert_int_to_float: mtc1 $t2,$f2 #from integer to float $f2 register destination 72 | cvt.s.w $f2, $f2 #$f2->1.0 73 | 74 | convert_float_to_int: cvt.w.s $f2,$f2 75 | mfc1 $t2,$f2 76 | 77 | *** If you want to transfer the binary number as is(without conversion to an integer) from a float register to an integer register 78 | (and vice versa) you can just use mtc1(move to coprocessor1) without the command cvt.w.s.(or in the other case cvt.s.w) -------------------------------------------------------------------------------- /useful-code/branches.s: -------------------------------------------------------------------------------- 1 | ---------------------- SWITCH CASE ---------------------------- 2 | 3 | switch_case_no_break: 4 | move $XY, $zero #initialize flag register $XY 5 | case01: bXX $YY,$ZZ, case02 #this condition/case is not valid, check next case 6 | if_case01: 7 | addi $XY, $XY, 1 8 | 9 | case02: bXX $YY,$ZZ, case?? #this condition/case is not valid, check next case 10 | if_case02: 11 | addi $XY, $XY, 1 12 | 13 | 14 | 15 | case0n: bXX $YY,$ZZ, case03 #this condition/case is not valid, check next case 16 | if_case0n: 17 | addi $XY, $XY, 1 18 | 19 | else_case: bne $XY,$zero, Exit #non of previous conditions/cases exit, exit 20 | if_non_case: 21 | 22 | 23 | 24 | switch_case_with_break: 25 | case001: bXX $YY,$ZZ, case02 #this condition/case is not valid, check next case 26 | if_case001: 27 | j Exit_switch_case 28 | 29 | case002: bXX $YY,$ZZ, case?? #this condition/case is not valid, check next case 30 | if_case002: 31 | j Exit_switch_case 32 | 33 | 34 | 35 | case00n: bXX $YY,$ZZ, else_case #this condition/case is not valid, check next case 36 | if_case00n: 37 | j Exit_switch_case 38 | 39 | else_case_: #non of previous conditions/cases exit 40 | if_non_case_: 41 | 42 | 43 | ---------------------------- WHILE ---------------------------- 44 | 45 | while_do_1_condition: 46 | bXX $YY,$ZZ, exit_while 47 | 48 | 49 | addi counter, counter, 1 #possible approach 50 | 51 | j while_do_1_condition 52 | 53 | while_do_a_AND_b: #when !a OR !b, exit while 54 | bXX $RR,$ZZ, exit_while if a is not valid exit while 55 | bYY $WW,$TT, exit_while if b is not valid exit while 56 | 57 | 58 | addi counter, counter, 1 #possible approach 59 | 60 | j while_do_a_AND_b 61 | 62 | while_do_a_OR_b_2: #when !a AND !b exit wile 63 | check_a: bXX $RR,$ZZ, check_b #if a is not valid check b 64 | 65 | do: 66 | addi counter, counter, 1 #possible approach 67 | j while_do_a_OR_b_2 68 | 69 | check_b: bYY $WW,$TT, exit_while #if b is not valid exit while 70 | 71 | j do 72 | 73 | ------------------------ IF ---------------------------- 74 | $s1 --> a , $s2 --> b 75 | 76 | if_a_equals_b_else: bne $s1, $s2,else 77 | ..... #instructions that will run if $s2=$s3 78 | j exit 79 | else: ..... #instructions that will run if $s2<>$s3 80 | exit: ..... 81 | 82 | General instructions about branches and their logic. Below work for if-commands mostly. 83 | 84 | $s0 == $s1 -----> bne $s0,$s1,label 85 | $s0 <> $s1 -----> beq $s0,$s1,label 86 | $s0 >= $s1 -----> blt $s0,$s1,label 87 | $s0 <= $s1 -----> bgt $s0,$s1,label 88 | $s0 > $s1 -----> ble $s0,$s1,label 89 | $s0 < $s1 -----> bge $s0,$s1,label 90 | 91 | In branches we put the opposite condition from the one that we want to apply(e.g. if we want $s1<>$s2 the branch command is 92 | beq $$s1,$s2,else). Below the branch command , we put all the commands that we want to run if the condition($s1<>$s2) 93 | is true(last command is always jump - j label so as to skip the label of the branch) and below them is the label(e.g. else). This 94 | label has all the commands that will run if the condition is false(the case of branch is valid). 95 | -------------------------------------------------------------------------------- /useful-code/other procedures/bit_isolation.s: -------------------------------------------------------------------------------- 1 | isolation of bit: 2 | li $t2,31 #like pointer 3 | li $t3, 0x80000000 #1000 0000 0000 0000 0000 0000 0000 0000 4 | li $t5, -1 5 | 6 | loop: and $s1,$s0,$t3 7 | srl $s1,$s1,$t2 8 | beqz $s1, add_zero 9 | if_is_1: addi $t1,$t1,1 10 | j fix 11 | add_zero: addi $t0,$t0,1 12 | fix: addi $t2,$t2,-1 13 | beq $t2, $t5, print_results 14 | srl $t3,$t3,1 15 | j loop -------------------------------------------------------------------------------- /useful-code/other procedures/char_isolation.s: -------------------------------------------------------------------------------- 1 | ->Print the first five chars of the string 2 | move $a0,$t1 3 | sb $zero, str2 + 5 #store byte 4 | syscall 5 | 6 | char_isolation: li $t0,0 #counter 7 | la $s0, string 8 | isolation_of_char_in_string: lbu $t1,0($s0) 9 | addi $s0,$s0,1 10 | addi $t0,$t0,1 11 | beq $t0, $XX, exit #XX register with length of the string 12 | j isolation_of_char_in_string #next char 13 | -------------------------------------------------------------------------------- /useful-code/other procedures/count_digits.s: -------------------------------------------------------------------------------- 1 | count_digits: move $t4, $t0 2 | li $t5,0 #number of digits 3 | li $t2, 10 4 | la $a0, powersum 5 | jal print_string 6 | digits: div $t4,$t2 7 | mflo $t4 8 | addi $t5,$t5, 1 #increase the number 9 | beqz $t4, internal_loop 10 | j digits -------------------------------------------------------------------------------- /useful-code/other procedures/min_max.s: -------------------------------------------------------------------------------- 1 | minmax: #as procedure 2 | #max 3 | move $v0, $a0 #v0 -> max 4 | first_comp : 5 | ble $a1,$v0,second_comp 6 | move $v0,$a1 7 | second_comp : 8 | ble $a2,$v0,third_comp 9 | move $v0,$a2 10 | third_comp : 11 | ble $a3,$v0, first 12 | move $v0,$a3 13 | #min 14 | first : 15 | move $v1, $a0 16 | bge $a1,$v1,second 17 | move $v1,$a1 18 | second : 19 | bge $a2,$v1,third 20 | move $v1,$a2 21 | third : 22 | bge $a3,$v1, exit 23 | move $v1,$a3 24 | exit: 25 | jr $ra 26 | 27 | -------------------------------------------------------------------------------- /useful-code/other procedures/num_power.s: -------------------------------------------------------------------------------- 1 | power_of_a_number: #s0->base 2 | #s1-> power 3 | li $t0,1 4 | li $s3,1 5 | pow: mul $s3,$s3,$s0 6 | addi $t0,$t0,1 7 | bgt $t0,$s1, print_results 8 | j pow 9 | -------------------------------------------------------------------------------- /useful-code/stack_and_functios.s: -------------------------------------------------------------------------------- 1 | -------------------------- STACK AND FUNCTIONS----------------------- 2 | We call functions with command jal (jump and link). This command saves in $ra the address that must return to, after the calling of the function. 3 | 4 | leaf_proc_not_using_stack: 5 | ## commands ## 6 | jr $ra 7 | 8 | leaf_proc_using_stack: 9 | addi $sp, $sp, -16 #3 words or 3 registers (xx, xy, zz & ra) --> 4*n=4*4=16 10 | 11 | sw $ra, 12 ($sp) 12 | sw $xx, 8 ($sp) 13 | sw $yy, 4 ($sp) 14 | sw $zz, 0 ($sp) 15 | 16 | ## commands ## 17 | 18 | lw $zz, 0 ($sp) 19 | lw $yy, 4 ($sp) 20 | lw $xx, 8 ($sp) 21 | lw $ra, 12 ($sp) 22 | 23 | addi $sp, $sp, 16 24 | 25 | jr $ra 26 | 27 | node_proc_using_stack: 28 | addi $sp, $sp, -16 #3 words or 3 registers (xx, xy, zz & ra) --> 4*n=4*4=16 29 | 30 | sw $ra, 12 ($sp) 31 | sw $xx, 8 ($sp) 32 | sw $yy, 4 ($sp) 33 | sw $zz, 0 ($sp) 34 | 35 | jal any_procedure #leaf_proc_non_using_stack or leaf_proc_using_stack or node_proc_using_stack 36 | 37 | lw $zz, 0 ($sp) 38 | lw $yy, 4 ($sp) 39 | lw $xx, 8 ($sp) 40 | lw $ra, 12 ($sp) 41 | 42 | addi $sp, $sp, 16 43 | jr $ra -------------------------------------------------------------------------------- /useful-code/system_call_codes.md: -------------------------------------------------------------------------------- 1 | # System Calls 2 | 3 | |Service|System Call codes|Arguments|Results| 4 | |:---|:---:|:---:|---:| 5 | |print_int|1|$a0=integer| | 6 | |print_float|2|$f12 = float| | 7 | |print_double|3|$f12 = double| | 8 | |print_string|4|$a0 = string| | 9 | |read_int|5| |integer (in $v0)| 10 | |read_float|6| |float (in $f0)| 11 | |read_double|7| |double (in $f0)| 12 | |read_string|8|$a0 = buffer, $a1 = length| | 13 | |sbrk|9|$a0 = amount|address (in $v0)| 14 | |exit|10| | | 15 | |print_char|11|$a0 = char| | 16 | |read_char |12| |char (in $v0)| 17 | |open|13|$a0 = filename (string), $a1 = flags, $a2 = mode|file descriptor (in $v0)| 18 | |read|14|$a0 = file descriptor, $a1 = buffer, $a2 = length|num chars read (in $v0)| 19 | |write|15|$a0 = file descriptor, $a1 = buffer, $a2 = length|num chars written (in $v0)| 20 | |close|16|$a0 = file descriptor| | 21 | |exit2|17|$a0 = result| | -------------------------------------------------------------------------------- /useful-code/template.s: -------------------------------------------------------------------------------- 1 | ################################################# 2 | # # 3 | # data segment # 4 | # # 5 | ################################################# 6 | 7 | .data 8 | endl: .asciiz "\n" 9 | ################################################# 10 | # # 11 | # text segment # 12 | # # 13 | ################################################# 14 | .text 15 | .globl __start 16 | __start: 17 | #-------------start of main program-------------# 18 | 19 | 20 | Exit: li $v0, 10 21 | syscall #au revoir... 22 | #-------------end of main program--------------# 23 | 24 | #-------------start of procedures--------------# 25 | print_endl: la $a0,endl 26 | li $v0, 4 #system call to print 27 | syscall 28 | jr $ra 29 | 30 | #-------------end of procedures---------------# 31 | 32 | 33 | 34 | 35 | 36 | --------------------------------------------------------------------------------