hw #1 problems & industrial strength assembly csci 136
TRANSCRIPT
![Page 1: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/1.jpg)
HW #1 Problems &Industrial Strength Assembly
CSCI 136
![Page 2: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/2.jpg)
Reading the Question“If there are no b’s in the string, then bfind
should return a pointer to the null character at the end of the string”
lbu $t2, 0($t3) beq $t2, $0, term …term: la $a0, ndone li $v0 ,4 syscallIts great that you tell me whats happening..
How about putting $t3 in to $v0?
![Page 3: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/3.jpg)
Reading the Question“The bfind procedure should locate the first b
character in the string and return its address in register $v0”
bfind: lbu $t1,0($a0)
beqz $t1, Ret
beq $t1, ‘b’, Ret
addi $a0, $a0, 1
j bfind
Ret: sb $v0, ($t1)
![Page 4: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/4.jpg)
Using the bfind procedure in bcount“You must use your bfind procedure in
Exercise 3.23 in your implementation of bcount.”
What does bfind return?
So how do we check if we’re at the end of the string?
jal bfindlb $t0,0($v0)beqz $t0, endOfString
![Page 5: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/5.jpg)
RTFQ – Reading the QuestionTough parts of Software:
1. Specifying Requirements
2. Understanding the Specified Requirements
Why it matters at GWU: Grading Scripts
Besides.. It should be really frustrating to spend hours answering the wrong question…
![Page 6: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/6.jpg)
RTFQ- Reading the QuestionTypical Errors:Didn’t write bcount as a procedure:
This requires saving $ra before the call to bfind, and restoring $ra after the call to bfind
main: jal bcount
bcount: jal bfindWhat happens to $ra when jal is called?
![Page 7: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/7.jpg)
Answering the Question add $t0, $0, $0
bfind: add $t3, $t0, $a0
lbu $t2, 0 ($t3)
beq $t2, $0, done
addi $t2, $t2, -98
beq $t2, $0, done
addi $t0, $t0, 1
j bfind
done: addi $t0,1
add $v0,$t0,$0
jr $ra
![Page 8: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/8.jpg)
Answering the Question li $t0,0
bfind: add $t3,$a0,$t0
lbu $t2, 0 ($t3)
beq $t2, $0, done
beq $t2, ‘b’, done
addi $t0, $t0, 4
j bfind
done: move $s2,$t0
jr $ra
![Page 9: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/9.jpg)
Why do we program in Assembly?
![Page 10: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/10.jpg)
Why do we program in Assembly?
What matters in real world:
SPEED, SPEED & SPEED
Other acceptable answer:Low Level Interface with hardware (mouse,
ethernet card, serial port, video card, etc.)Unfortunately we can’t simulate these
hardware interfaces with SPIM.
![Page 11: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/11.jpg)
Naïve Approach to Style
“Good Style” == lots of comments
Even poor code well
documented is still
poor code.
![Page 12: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/12.jpg)
Style• Accomplish task in straight forward manner
with minimum effort
• Unfortunately, “Good Style” comes from experience and experience comes from “Bad Style”
We don’t expect you to have wonderful style in the first couple assignments…
By the end of semester you should be writing simple straight forward code.
![Page 13: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/13.jpg)
Minimum Effort
la $t0, $a0bfind: lbu $t1, 0 ($t0) beq $t1, Done beq $t1,’b’, Done addi $t0, 1 addi $a0, 1 j bfind
Done: move $v0,$a0
![Page 14: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/14.jpg)
Minimum EffortRegisters are a Commodity. Do not waste
them!Why is minimizing the use of registers so
important?
What is the slowest type of instruction on our processor?
If we have registers available... We can use them.
This will allow your programs to run faster!
![Page 15: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/15.jpg)
Minimum EffortRegisters are a Commodity. Do not waste
them!Why is minimizing the use of registers so important?
Modern processors have multiple execution units…
Processors can execute several different portions of your code at the same time...
Lack of registers prevents multiple execution…
Help the processor out by using minimal number of registers and having minimal register “overlap”.
This will allow your programs to run faster!
![Page 16: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/16.jpg)
Do we need all these registers? li $s1,0x00
bfind:
lbu $t2,0($a0)
beq $t2,$s1,exit
beq $t2,’b’,exit
addi $a0,$a0,1
j bfind
exit:
![Page 17: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/17.jpg)
Do we need all this code?bfind:
lbu $t2,0($a0)
addi $a0,$a0,1
beq $t2,$zero,exit
beq $t2,’b’,exit
j bfind
exit:
subu $a0,$a0,1
![Page 18: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/18.jpg)
Minimum EffortKnow your architecture (load - store) and
what your pseudo instructions do:
Is there a difference difference between:
beq $t0, ‘b’, Done
and
addi $at,$zero,’b’
beq $t0,$at, Done
![Page 19: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/19.jpg)
Do I need everything in my loop?bfind: lbu $t0, 0 ($a0)
beqz $t0, done
addi $t1,$zero,’b’
beq $t0, $t1, done
add $a0, $a0, 1
j bfind
done: mov $v0, $a0
jr $ra
![Page 20: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/20.jpg)
Extra Branches & Border Case Failurebfind: li $t1, ‘b’ lb $t0,0($a0) bne $t0,$t1,loop move $v0,$a0quit: jr $raloop: addi $a0,$a0,1 lbu $t0,0($a0) beq $t0,$zero,quit bne $t0,$t1,loop move $v0,$a0 jr $ra
![Page 21: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/21.jpg)
Extra Branches and Loops
Why are extra branches bad?
-Optimizing Branches
Why are extra loops bad?
![Page 22: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/22.jpg)
Public and Private VariablesIn SPIM- When you call a function… and the function
returns… the only values from that function your procedure should access are:
$v0, $v1
Values saved by the function on the stackLocations in Memory
DO NOT access the internal variables (registers) of another function! This “breaks” the whole “function” paradigm. It isn’t a “black box” if you go digging around…
![Page 23: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/23.jpg)
Public and Private Variables
FUNCTION
GWU
![Page 24: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/24.jpg)
Breaking the Function Paradigmaddi $sp, $sp, -4
sw $ra,0($sp)
jal bfind
lw $ra,0($sp)
addi $sp,$sp,4
quit: bne $t0, $zero, adder
Who set $t0? bfind did. But the only return value was supposed to be $v0?
What are the problems with this?
![Page 25: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/25.jpg)
Breaking the Function Paradigm
I should be able to plug in ANY bfind in to bcount and make it work.
Don’t fight the framework!
![Page 26: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/26.jpg)
Real World Disclaimer…Register assignments aren’t as structured…(no $t0-$t9, $s0-$s7..just registers $1-$31)
Saving values on stack or in memory is slow.
More registers can be used to return values.
Function writers and function callers typically have specific agreements about what registers to use for passing and returning values.
![Page 27: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/27.jpg)
Proper Instructionsjr $1 is jump to a value given by the register $1
(Where do you typically see jr $ra ?)
j is jump to a label
Use addi to add a constant to a register
Use add to add registers together
Watch the ordering of registers in instruction…
sw $sp, $ra What does this do?
![Page 28: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/28.jpg)
Good Implementation of Bfind addi $t1,$zero,’b’bfind: lbu $t0, 0 ($a0) beqz $t0, done beq $t0, $t1, done add $a0, $a0, 1 j bfind
done: move $v0, $a0 jr $ra8 lines of assembly. No extra branches,
loops, variables. Common case is fast.
![Page 29: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/29.jpg)
Lab Assignment: Endian Fixer• TCP/IP message headers are in Big Endian• Your PC is a Little Endian machine• Write function that takes word value in $a0 and
returns a Endian reversed value in $v0• Goals:
– Learn how to use masks
li $t0, 0x000000ff
andi $t1, $a0, $t0
ori $v0, $t0, $t1
![Page 30: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/30.jpg)
endianFixer: #1234rol $a0,$a0,8 #2341lui $t1, 0x00ffori $t1, $t1, 0x00ffand $v0,$t1,$a0 #0301
sll $t1,$t1,8 #$t1=ff00ff00rol $a0,$a0,16 #4123and $a0,$t1,$a0 #4020or $v0,$v0,$a0 #4321
jr $ra Why don’t I ever zeroize $v0?Is li $t1,0x00ff00ff ok?
![Page 31: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/31.jpg)
Adder Review• How many delays does a 4 bit ripple carry
adder take to add two 4 bit numbers? Why?
![Page 32: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/32.jpg)
Adder ReviewWhat are the equations for a 4 bit carry look
ahead adder? Do you understand this?
![Page 33: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/33.jpg)
From when a and b’s show up… how long does it take until sum appears on the output lines?
How long for carry generation + how long for sum?
![Page 34: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/34.jpg)
8 bit adderWe need an 8 bit adder…how can we do this?
![Page 35: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/35.jpg)
8 bit adder We could have a pure ripple carry…
How long would this take to calculate
the sums?
![Page 36: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/36.jpg)
8 bit adder We could use ripple carry to connect
two 4 bit carry look ahead groups…
How long would this take to calculate
the carries? The sums?
![Page 37: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/37.jpg)
2nd layer of Carry LookaheadLets add another layer of Carry Lookahead for
our 8 bit adder…
What are the equations for big Ps?
What are the equations for big Gs?
What are the equations for the big Cs?
![Page 38: HW #1 Problems & Industrial Strength Assembly CSCI 136](https://reader034.vdocuments.net/reader034/viewer/2022051516/56649eb35503460f94bbb206/html5/thumbnails/38.jpg)
2nd layer of Carry LookaheadHow long would it take to calculate the carries?
How long would it take to calculate the sums?