Open book You may consult your textbook, my printed class notes, and one page of information you have prepared. You may also use a printed dictionary. You may not use a calculator or any other electronic device. Do not copy any wrong answers!
F F F F E 6 0(Corrected. Thank you, Simon.)
0 0 0 0 1 9 F - one's complement 0 0 0 0 1 A 0 - two's complement
1 *162 = 256
10 *16 = 160
0
416, so the number is -416
444/16 = 27 R 12 = C
27/16 = 1 R 11 = B
1/16 = 0 R 1 = 1 The hex number is 0x1BC or 1 1011 1100
bgez $a0, positive abs $v0, $a0 #absolute value of negative number li $v1, 0 # 0 as directed j endgoodgrief #skip the "else" part positive: sra $v0, $a0, 7 #divide by 27 = 128 mul $v0, $v0, $v0 # quotient squared and $v1, $a0, 0x7f # remainder is rightmost 7 bits endgoodgrief:There would be other solutions. neg would do here for abs, and of course div would give a quotient and remainder.
nor $v0, $t0
slt $t7, $t0, 'A' #'A' is character after '@'
beqz $t7, maybeletter # branch if $t0 is >= 'A'
la $a0, another
li $v0, 4
loop: syscall # first time, prints another
sub $a0,5 # now a0 points somewhere inside "Help!", probably the e
sub $v0,3 # vO is now 1, the print int service
bne $v0, 1, loop # well, it doesn't branch, does it?
li $v0,10 # ...and we are finished
syscall
.dataWell, that wasn't much fun, was it? Had the branch been different, it would have attempted to print an int the second time, and then perhaps an infinite loop?
string: .asciiz "Help!"
another: .asciiz "I need somebody\n"
Suppose that $a0 points to an array of 365 words, containing
daily temperatures. Make $v0 be the count of days of extreme
temperature, that is, less than -10 or greater than +25
li $t0, 365 #days of data li $v0, 0 #bad day count tloop: beqz $t0, endt #finished? sub $t0, 1 # one less day to do lw $t1, ($a0) # get the temperature for this day slt $t2, $t1, -10 #really cold! slt $t3, $t1, 26 #not too hot xor $t2, $t2, $t3 #true if not really cold and not too hot bnez $t2, niceday add $v0, 1 # count an extreme day niceday: add $a0, 4 #point to next array element (don't forget this!) j tloop endt: #all done counting.
#define SIZE 7 int a[SIZE] = { 1, 3, 5, 7, 9, 88, -1}; int b[SIZE] = {11,-5, 5, 3, -29, -8, 43}; int main() { int sum = 0; int this; for (int i=0; i < SIZE; i++) { /*go thru arrays*/ this = a[i] + b[i]; /*adding the corresponding elements*/ printf(" %d,",this); sum += this; } printf ("\n It all adds up to %d\n", sum); /*note newlines */ return 0; }[jensen@shevek temp]$ gcc -std=c99 samp.c
[jensen@shevek temp]$ ./a.out
12, -2, 10, 10, -20, 80, 42,
It all adds up to 132