CS61C Starting a Program Lecture 10 February 19 1999 Dave Patterson http cs berkeley edu patterson www inst eecs berkeley edu cs61c schedule html cs 61C L10 Start 1 Patterson Spring 99 UCB Review 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 flag cs 61C L10 Start 2 Patterson Spring 99 UCB Review 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 3 Patterson Spring 99 UCB Outlin e Floating Point Questions and Answers Compiling Assembling a Program Administrivia Computers in the News Linking Loading a Program An Example Conclusion cs 61C L10 Start 4 Patterson Spring 99 UCB Floating Point Questions Negative 0 What is result of this program main float i j Output i 1 0 j 0 0 Inf printf 3 1f n i j printf 3 1f n i j Inf printf 3 1f n j i j 0 0 if j i j 0 0 0 0 0 0 printf 0 0 0 0 n else printf 0 0 0 0 n NaN printf 3 1f n i j i j printf 3 1f n i j i j i NaN cs 61C L10 Start 5 Patterson Spring 99 UCB Big Idea Levels of Abstaction Unix DOS C program foo c foo txt Compiler Assembly program foo s foo asm Assembler Object mach lang module foo o foo obj Linker lib o lib obj Executable mach lang pgm a out foo exe cs 61C L10 Start 6 Loader Memory Patterson Spring 99 UCB Assemble r Follow Directions Replace Pseudoinstructions Find list of labels addresses Produce machine language Creates Object File cs 61C L10 Start 7 Patterson Spring 99 UCB Example Assembly Program Directives text align 2 globl main main subu sp sp 32 sw ra 20 sp sd a0 32 sp sw 0 24 sp sw 0 28 sp loop lw t6 28 sp mul t7 t6 t6 lw t8 24 sp addu t9 t8 t7 sw t9 24 sp cs 61C L10 Start 8 addu t0 t6 1 sw t0 28 sp ble t0 100 loop la a0 str lw a1 24 sp jal printf move v0 0 lw ra 20 sp addiu sp sp 32 j ra data align 0 str asciiz The sum from 0 100 is d n Patterson Spring 99 UCB Assembler Directives p A 51 to A53 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 it cs 61C L10 Start 9 Patterson Spring 99 UCB Example Assembly Program PseudoInstr text align 2 globl main main subu sp sp 32 sw ra 20 sp sd a0 32 sp sw 0 24 sp sw 0 28 sp loop lw t6 28 sp mul t7 t6 t6 lw t8 24 sp addu t9 t8 t7 sw t9 24 sp cs 61C L10 Start 10 addu t0 t6 1 sw t0 28 sp ble t0 100 loop la a0 str lw a1 24 sp jal printf move v0 0 lw ra 20 sp addiu sp sp 32 j ra data align 0 str asciiz The sum from 0 100 is d n Patterson Spring 99 UCB Pseudoinstruction replacements treats common variations of machine Asm 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 t6 mflo t7 addu t0 t6 1 addiu t0 t6 1 ble t0 100 loop slti at t0 101 bne at 0 loop la a0 str lui at left str ori a0 at right str move v0 0 add v0 0 0 cs 61C L10 Start 11 Patterson Spring 99 UCB Example Assembly Program Labels text align 2 globl main main subu sp sp 32 sw ra 20 sp sd a0 32 sp sw 0 24 sp sw 0 28 sp loop lw t6 28 sp mul t7 t6 t6 lw t8 24 sp addu t9 t8 t7 sw t9 24 sp cs 61C L10 Start 12 addu t0 t6 1 sw t0 28 sp ble t0 100 loop la a0 str lw a1 24 sp jal printf move v0 0 lw ra 20 sp addiu sp sp 32 j ra data align 0 str asciiz The sum from 0 100 is d n Patterson Spring 99 UCB Addresses Main job of Assembler 1 Find list of labels and their addresses 2 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 addresses cs 61C L10 Start 13 Patterson Spring 99 UCB Administrivia 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 MIPS Simulator Due Wed 3 3 7PM deadline Thurs 8AM Twice semester 24 hour extension Everything but midterm may be done in pairs now Solutions cs61c solutions directory Access it using cs61c account Not www Midterm conflict time Mon 3 15 6 9PM cs 61C L10 Start 14 Patterson Spring 99 UCB Computers in the News Microsoft Denies Rumors That It Is Crafting a Language Like …
View Full Document
Unlocking...