CompOrg HW4 FAQ


Question:

How do I convert from an ASCII hex digit to 4 bits?

Answer:

Don't think of it as converting to bits, think of just extracting the value that corresponds to the ASCII hex digit. The ASCII hex digit '0' (ASCII code 48) corresponds to the value 0, '1' (49) corresponds to 1, 'A' (65) is the value 10, 'B' (66) is 11, etc. You could simply use a bunch of if statements or a table lookup, but you can also use arithmetic:

Assume c is a char that holds some ASCII encoded hex digit ('0'..'9','A'..'F').

If c is ≤ '9', then the value can be expressed as c - '0'.

If c is not ≤ '9' then the value can be expressed as c - 'A' + 10.


Question:

I don't think I need all the subroutines you list in the project description, for example, I can convert a string of ASCII HEX digits to a word with one subroutine. Is this OK?

Answer:

My suggested breakdown of subroutines was intended to make the lowest level subroutine as simple as possible (to get people started). In my case the subroutine that converts one hex digit to a nibble is about the simplest operation I think makes sense to put in a subroutine... More complex subroutines are great - go for it!


Question:

Can you provide an example of how to use syscall to read a string?

Answer:

         .data
prompt:  .asciiz"Enter a string "
msg:     .asciiz "Your string was :"
buff:    .space 20

         .text
         .align 4
	 .globl main

main:
         sub $sp,$sp,4      # main can return, so save $ra
         sw $ra,0($sp)

	 li $v0,4           # print out prompt
	 la $a0, prompt
	 syscall

	 li $v0,8           # read string
	 la $a0, buff       # where the string should be put
	 li $a1,10          # Maximum number of chars
	 syscall

# buff now contains the string entered by the user,

         li $v0,4           # print out msg
	 la $a0,msg
	 syscall

	 li $v0,4           # print out user string
	 la $a0,buff
	 syscall

	 lw $ra,0($sp)      # return from main
	 add $sp,$sp,4
	 jr $ra