Practice Midterm

  1. Here is a binary number: 01000011
  2. Convert to signed decimal the number 0xFFFFFFE3

    It is negative. 2's complement 0x1c+1 = 0x1d, so number is -39
  3. X, Y, and Z are numbers. Write an assembly language program to print the result of (X + Y) / Z , and then a string labeled finish:
    lw $t0, X
    lw $t1, Y
    lw $t2, Z
    add $a0, $t0, $t1 #a0 = X+Y
    div $a0, $a0, $t2 #a0 = (X+Y)/Z
    li $v0, 1 # print int service
    syscall # print result that's in $a0

    la $a0, finish # print the final string
    li $v0, 4
    syscall

    li $v0, 10 # exit program
    syscall

  4. Write a function whose argument (in $a0) is the address of a string. It should return (in $v0) the number of 'e's in the string.
    count_es:
    li $v0, 0 # v0 holds count of 'e's
    celoop:
    lb $t0, ($a0) # in string addressed by a0
    beqz $t0, endce # end of string
    bne $t0, 'e', not_e # if its an 'e'
    add $v0, 1 # count it
    not_e:
    add $a0, 1 # point to next char
    j celoop
    endce:
    jr $ra # return count in $v0

  5. Write a function whose arguments are the address of an array of words, and the length of that array (in $a1). Return the maximum value in the array.
    max:
    lw $v0, ($a0) #max = a[0] first array element
    mloop:
    sub $a1, 1 #count--
    add $a0, 4 #a++ point to next word
    blez $a1, mend # no more words
    lw $t0, ($a0) # get next word
    ble $t0, $v0, notgreater
    move $v0, $t0 #found new max
    notgreater:
    j mloop # keep looking
    mend:
    jr $ra #return with max in $v0
  6. for this instruction, using the information on pp. 60-61, show how it is encoded in binary, and hexadecimal.
    	sub $t3, $t1, $t2
	000000 01001  01010 01011  00000 100010
0000 0001 0010 1010 0101 1000 0010 0010
0 1 2 A 5 8 2 2