CS61C Starting a Program Lecture 10Review: New MIPS arithmetic instructionsReview 2/2OutlineFloating Point Questions: Negative 0?Big Idea: Levels of Abstaction (Unix/DOS)AssemblerExample Assembly Program: DirectivesAssembler Directives (p. A-51 to A-53)Example Assembly Program: PseudoInstrPseudoinstruction replacementsExample Assembly Program: LabelsAddressesAdministrivia“Computers in the News”Slide 16Object File Format (output of Assembler)Link Editor or LinkerSteps of LinkerC memory allocation seen by the ProgramAbsolute Addresses in MIPSSlide 22LoadingSteps 1-5 (out of 6) for loading programSteps 6 (out of 6) for loading programExample: C Asm Obj Exe RunExample: C Asm Obj Exe RunSymbol Table EntriesSlide 29Slide 30Slide 31Slide 32“And in Conclusion..” 1/1cs 61C L10 Start.1Patterson Spring 99 ©UCBCS61CStarting a Program Lecture 10February 19, 1999Dave Patterson (http.cs.berkeley.edu/~patterson)www-inst.eecs.berkeley.edu/~cs61c/schedule.htmlcs 61C L10 Start.2Patterson Spring 99 ©UCBReview: New MIPS arithmetic instructions°Example Meaning Comments°mult $2,$3 Hi, Lo = $2 x $3 64-bit signed product°multu$2,$3 Hi, Lo = $2 x $3 64-bit unsigned product°div $2,$3 Lo = $2 ÷ $3, Lo = quotient, Hi = rem°divu $2,$3 Lo = $2 ÷ $3, Unsigned quotient, rem.°mfhi $1 $1 = Hi Used to get copy of Hi°mflo $1 $1 = Lo Used to get copy of Lo°add.s $0,$1,$2 $f0=$f1+$f2 Fl. Pt. Add (single)°add.d $0,$2,$4 $f0=$f2+$f4 Fl. Pt. Add (double)°sub.s $0,$1,$2 $f0=$f1-$f2 Fl. Pt. Subtract (single)°sub.d $0,$2,$4 $f0=$f2-$f4 Fl. Pt. Subtract (double)°mul.s $0,$1,$2 $f0=$f1x$f2 Fl. Pt. Multiply (single)°mul.d $0,$2,$4 $f0=$f2x$f4 Fl. Pt. Multiply (double)°div.s $0,$1,$2 $f0=$f1÷$f2 Fl. Pt. Divide (single)°div.d $0,$2,$4 $f0=$f2÷$f4 Fl. Pt. Divide (double)°c.X.s $0,$1 flag1= $f0 X $f1 Fl. Pt.Compare (single)°c.X.d $0,$2 flag1= $f0 X $f2 Fl. Pt.Compare (double) X is eq, lt, le; bc1t, bc1f tests flagcs 61C L10 Start.3Patterson Spring 99 ©UCBReview 2/2°IEEE 754 Floating Point standard: accuracy first class citizen°Multiply product 2n bits; Divide produces both n-bit quotient and n-bit remainder°Computer numbers have limited size limited precision •underflow: too small for Fl. Pt. (bigger negative exponent than can represent) •overflow: too big for Fl. Pt. or integer (bigger positive exponent than can represent, or bigger integer than fits in word) •Programmers beware!cs 61C L10 Start.4Patterson Spring 99 ©UCBOutline°Floating Point Questions and Answers°Compiling, Assembling a Program°Administrivia, “Computers in the News”°Linking, Loading a Program°An Example°Conclusioncs 61C L10 Start.5Patterson Spring 99 ©UCBFloating Point Questions: Negative 0?°What is result of this program?main() {float i,j;i = 1.0; j = 0.0;printf("%3.1f\n",i/j);printf("%3.1f\n",-i/j);printf("%3.1f\n",j/(-i/j));if (j/(-i/j) == 0.0) printf("0.0 == -0.0\n");else printf("0.0 != -0.0\n");printf("%3.1f\n",i/j-i/j);printf("%3.1f\n",(i/j-i/j)+i);}°Output:Inf-Inf-0.00.0 == -0.0NaNNaNcs 61C L10 Start.6Patterson Spring 99 ©UCBBig Idea: Levels of Abstaction (Unix/DOS)C program: foo.c/foo.txtAssembly program: foo.s/ foo.asmExecutable(mach lang pgm): a.out/foo.exeCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.o/foo.objlib.o/lib.objcs 61C L10 Start.7Patterson Spring 99 ©UCBAssembler°Follow Directions°Replace Pseudoinstructions°Find list of labels, addresses°Produce machine language°Creates Object Filecs 61C L10 Start.8Patterson Spring 99 ©UCBExample Assembly Program: Directives.text.align 2.globl mainmain:subu $sp,$sp,32sw $ra, 20($sp)sd $a0, 32($sp)sw $0, 24($sp)sw $0, 28($sp)loop:lw $t6, 28($sp)mul $t7, $t6,$t6lw $t8, 24($sp)addu $t9,$t8,$t7sw $t9, 24($sp) addu $t0, $t6, 1sw $t0, 28($sp)ble $t0,100, loopla $a0, strlw $a1, 24($sp)jal printfmove $v0, $0lw $ra, 20($sp)addiu $sp,$sp,32j $ra.data.align 0str:.asciiz "The sum from 0 .. 100 is %d\n"cs 61C L10 Start.9Patterson Spring 99 ©UCBAssembler Directives (p. A-51 to A-53)°Tell assembler how to translate program but do not produce machine instructions .text (addr): Subsequent items put in user text segment (starting at addr) .align n: Align the next data on a 2n byte boundary; align 2 next word boundary .globl sym: declares sym global and can be referenced from other files .data (addr): Subsequent items put in user data segment (starting at addr) .asciiz str: Store the string str in memory and null-terminate itcs 61C L10 Start.10Patterson Spring 99 ©UCBExample Assembly Program: PseudoInstr.text.align 2.globl mainmain:subu $sp,$sp,32sw $ra, 20($sp)sd $a0, 32($sp)sw $0, 24($sp)sw $0, 28($sp)loop:lw $t6, 28($sp)mul $t7, $t6,$t6lw $t8, 24($sp)addu $t9,$t8,$t7sw $t9, 24($sp) addu $t0, $t6, 1sw $t0, 28($sp)ble $t0,100,loopla $a0, strlw $a1, 24($sp)jal printfmove $v0, $0lw $ra, 20($sp)addiu $sp,$sp, 32j $ra.data.align 0str:.asciiz "The sum from 0 .. 100 is %d\n"cs 61C L10 Start.11Patterson Spring 99 ©UCBPseudoinstruction replacements°Asm. treats common variations of machine language instructions as if real instructions•subu $sp,$sp,32 addiu $sp,$sp,-32•sd $a0, 32($sp) sw $a0, 32($sp)sw $a1, 36($sp)•mul $t7,$t6,$t6 mul $t6,$t6mflo $t7•addu $t0,$t6,1 addiu $t0,$t6,1•ble $t0,100,loop slti $at,$t0,101bne $at,$0,loop•la $a0, str lui $at,left(str) ori $a0,$at,right(str)•move $v0, $0 add $v0,$0,$0cs 61C L10 Start.12Patterson Spring 99 ©UCBExample Assembly Program: Labels.text.align 2.globl mainmain:subu $sp,$sp,32sw $ra, 20($sp)sd $a0, 32($sp)sw $0, 24($sp)sw $0, 28($sp)loop:lw $t6, 28($sp)mul $t7, $t6,$t6lw $t8, 24($sp)addu $t9,$t8,$t7sw $t9, 24($sp) addu $t0, $t6, 1sw $t0, 28($sp)ble $t0,100, loopla $a0, strlw $a1, 24($sp)jal printfmove $v0, $0lw $ra, 20($sp)addiu $sp,$sp,32j $ra.data.align 0str:.asciiz "The sum from 0 .. 100 is %d\n"cs 61C L10 Start.13Patterson Spring 99 ©UCBAddresses°Main job of Assembler:1) Find list of labels and their addresses2) Produce machine language°Puts label and memory address of that instruction in Symbol Table°Can use a name before it is defined; forward reference e.g., str in example•Note: Must declare before use in C°Uses Symbol Table in 2nd pass to produce machine code, including addressescs 61C L10 Start.14Patterson Spring 99 ©UCBAdministrivia°Readings: (3.9) A.2, A.3, A.4, 3.10, 3.11°5th homework: Due 2/24 7PM•Exercises 4.21, 4.25, 4.28°3rd Project/5th Lab:
View Full Document