Search code examples
assemblywhile-loopbit-shiftmips32instruction-set

mips assembly code to shift the bit programming


Read in your Student ID and save it to a register, and read in the number “10010000x” as the initial memory address. Then shift your student ID number to the right one bit at a time, and save it to the memory address which is 4 bytes increment from the previous memory address, until your student ID number is smaller than 1, then save your final student ID number which is NOT smaller than 1, and save the last memory address, and how many iterations to 3 different memory addresses. Turn in the hardcopy of the MARS graphic printout which has the MIPS Assembly code showing your student ID, and memory addresses and all registers values, and also answer the following three questions on the hard copy paper :
(1) How many shift right you have to do to get your student ID number to be smaller than 1 ? (2) What is your last NOT smaller than 1 binary student id number ? (3) What is the last memory address that saves that last NOT smaller than 1 student ID ?


Solution

  • li $t0, 0x10010000
    li $t1, student id
    li $s3, 1
    li $s5, 1
    add $s0, $t0, $zero
    add $s1, $t1, $zero
    loop1 : sw $s1, 0($s0)
    srl $s2, $s1, 1
    slt $s4, $s2, $s3
    beq $s4, $s3, L2
    addi $s0, $s0, 4
    add $s1, $s2, $zero
    add $s5,$s5,$s3
    j  loop1
    L2 : addi $s0, $s0, 4
    sw $s5, 4($s0)
    sw $s2, 8($s0)
    addi $t3, $s0, -4
    sw $t3, 12($s0)