Computer Organization Fall 2000

Homework 3
Frequently Asked Questions

Questions:
Reading a string Loop Division Negative Numbers Extra Credit Subroutines & Saving Registers

Q: I can't read the operation properly, can you post some code to call the read_string syscall?

A: OK. First you need to declare some space in memory to put the string and associate a label with that memory. In the code below this is the line buf: .space 10. Then you need to put 8 in $v0, the address of buf in $a0 and the maximum string length you want to read including the null in $a1. If you try to use a length of 1 the syscall will return immediately, since it needs 1 to store the terminating null, so use at least a value of 2!

.data
prompt:   .asciiz  "Enter an operation"
buf:      .space 10   # reserves 10 bytes

          .text
          .align 2

main:
      li $v0, 4          # syscall 4 is print string
      la $a0, prompt
      syscall

      li $v0, 8          # syscall 8 is read string
      la $a0,buf
      li $a1,2           # reads 1 char plus the null
      syscall

      # the first character is now at address buf (still in $a0)
      lbu $t0,0($a0)     # go get the first char

Q: Should our program loop forever, handling multiple operations? Or should it just quit when a single operation has been completed?

A: Either way is fine. It's probably easier for you to debug if it has a loop that allows the user to try as many operations as desired.

Q: What is the result of Integer Division? Do I need to worry about division by 0?

A: You just need to print out the quotient. You can assume that division by 0 will not be tested.

Q: Should our program correctly handle negative numbers (particularly in divide and multiply)?

A: It's not necessary, you can assume everything is non-negative.

Q: Is there any extra credit?

A: Sure: Avoid division by 0 and support negative numbers.

Q:Hi. I am just confused as to which registers "callers" are supposed to save and restore as compared to which registers "callees" are supposed to save and restore. The multiply subroutine you provided saves $t0, but I thought you didn't need to save $t0-7 ?

A: The convention is that a subroutine should save and restore $s0-7, but does not need to worry about $t0-7. My multiply subroutine should not save $t0 (there is no reason for it to do so, but it still works fine).