2/8/11%1%CS%61C:%Great%Ideas%in%Computer%Architecture%(Machine%Structures)%Instruc(ons*as*Numbers*Instructors:%Randy%H.%Katz%David%A.%PaGerson%hGp://inst.eecs.Berkeley.edu/~cs61c/fa10%1%Spring%2011%OO%Lecture%#7%2/8/11%2/8/11% Spring%2011%OO%Lecture%#7% 2%Levels%of%RepresentaSon/InterpretaSon%lw %%%$t0,%0($2)%lw %%%$t1,%4($2)%sw %%%$t1,%0($2)%sw %%%$t0,%4($2)%High%Level%Language%Program%(e.g.,%C)%Assembly%%Language%Program%(e.g.,%MIPS)%Machine%%Language%Program%(MIPS)%Hardware%Architecture%DescripCon%(e.g.,%block%diagrams)%%Compiler)Assembler)Machine)Interpreta4on)temp%=%v[k];%v[k]%=%v[k+1];%v[k+1]%=%temp;%0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 !Logic%Circuit%DescripCon%(Circuit%SchemaCc%Diagrams)%Architecture)Implementa4on)Anything%can%be%represented%as%a%number,%%i.e.,%data%or%instrucSons%2/8/11% 3%Spring%2011%OO%Lecture%#7%We*are*here!*Agenda%• Everything%is%a%Number%• Administrivia%• Overflow%and%Real%Numbers%• InstrucSons%as%Numbers%• Technology%Break%• Assembly%Language%to%Machine%Language%• Summary%2/8/11% Spring%2011%OO%Lecture%#7% 4%Agenda%• Everything%is%a%Number%• Administrivia%• Overflow%and%Real%Numbers%• InstrucSons%as%Numbers%• Technology%Break%• Assembly%Language%to%Machine%Language%• Summary%2/8/11% Spring%2011%OO%Lecture%#7% 5%Key%Concepts%• Inside%computers,%everything%is%a%number%• But%everything%is%of%a%fixed%size%– 8Obit%bytes,%16Obit%half%words,%32Obit%words,%64Obit%double%words,%…%• Integer%and%floaSng%point%operaSons%can%lead%to%results%too%big%to%store%within%their%representaSons:%overflow/underflow*2/8/11% Spring%2011%OO%Lecture%#7% 6%2/8/11%2%Number%RepresentaSon%• Value%of%iOth%digit%is%d*×*Basei*where%i%starts%at%0%and%increases%from%right%to%led:%• 12310%=%110%x%10102%+%210%x%10101%+%310%x%10100%% %%=%1x10010%+%2x1010%+%3x110%%=%10010%+%2010%+%310%% %%=%12310%%• Binary%(Base%2),%Hexadecimal%(Base%16),%Decimal%(Base%10)%different%ways%to%represent%an%integer%– We%use%1two,%5ten,%10hex%to%be%clearer%%% %(vs.%12,%%%%48,%%%510,%%1016%%)%2/8/11% Spring%2011%OO%Lecture%#7% 7%Number%RepresentaSon%• Hexadecimal%digits:%0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F%• FFFhex%=%15tenx%16ten2%+%15tenx%16ten1%+%15tenx%16ten0%%%% %=%3840ten%+%240ten%+%15ten%%% %=%4095ten%• 1111%1111%1111two%=%FFFhex%=%4095ten%• May%put%blanks%every%group%of%binary,%octal,%or%hexadecimal%digits%to%make%it%easier%to%parse,%like%commas%in%decimal%2/8/11% Spring%2011%OO%Lecture%#7% 8%Signed%and%Unsigned%Integers%• C,%C++,%and%Java%have%signed*integers,%e.g.,%7,%O255:%int x, y, z; • C,%C++%also%have%unsigned*integers,%which%are%used%for%addresses%• 32Obit%word%can%represent%232%binary%numbers%• Unsigned%integers%in%32%bit%word%represent%%0%to%232O1%(4,294,967,295)%2/8/11% Spring%2011%OO%Lecture%#7% 9%Unsigned%Integers%0000%0000%0000%0000%0000%0000%0000%0000two%=%0ten%0000%0000%0000%0000%0000%0000%0000%0001two%=%1ten%0000%0000%0000%0000%0000%0000%0000%0010two%=%2ten%% %...% %%...%0111%1111%1111%1111%1111%1111%1111%1101two%=%2,147,483,645ten%0111%1111%1111%1111%1111%1111%1111%1110two%=%2,147,483,646ten%0111%1111%1111%1111%1111%1111%1111%1111two%=%2,147,483,647ten%1000%0000%0000%0000%0000%0000%0000%0000two%=%2,147,483,648ten%1000%0000%0000%0000%0000%0000%0000%0001two%=%2,147,483,649ten%1000%0000%0000%0000%0000%0000%0000%0010two%=%2,147,483,650ten%% %...% %...%1111%1111%1111%1111%1111%1111%1111%1101two%=%4,294,967,293ten%1111%1111%1111%1111%1111%1111%1111%1110two%=%4,294,967,294ten%1111%1111%1111%1111%1111%1111%1111%1111two%=%4,294,967,295ten%2/8/11% Spring%2011%OO%Lecture%#7% 10%Signed%Integers%and%%Two’s%Complement%RepresentaSon%• Signed%integers %in %C;%want%½%numbers%<0,%want%½%numbers%>0,%and%want%one%0%%• Two’s*complement*treats%0%as%posiSve,%so%32Obit%word%represents%232%integers%from%O231%(–2,147,483,648)%to%231O1%(2,147,483,647)%– Note:%one%negaSve%number%with%no%posiSve%version%– Book%lists%some%other%opSons,%all%of%which%are%worse%– Every%computers%uses%two’s%complement%today%• Most*significant*bit*(ledmost)%is%the%sign*bit,%since%0%means%posiSve%(including%0),%1%means%negaSve%– Bit%31%is%most%significant,%bit%0%is%least%significant%2/8/11% Spring%2011%OO%Lecture%#7% 11%Two’s%Complement%Integers%0000%0000%0000%0000%0000%0000%0000%0000two%=%0ten%0000%0000%0000%0000%0000%0000%0000%0001two%=%1ten%0000%0000%0000%0000%0000%0000%0000%0010two%=%2ten%% %...% %...%0111%1111%1111%1111%1111%1111%1111%1101two%=%2,147,483,645ten%0111%1111%1111%1111%1111%1111%1111%1110two%=%2,147,483,646ten%0111%1111%1111%1111%1111%1111%1111%1111two%=%2,147,483,647ten%1000%0000%0000%0000%0000%0000%0000%0000two%=%–2,147,483,648ten%1000%0000%0000%0000%0000%0000%0000%0001two%=%–2,147,483,647ten%1000%0000%0000%0000%0000%0000%0000%0010two%=%–2,147,483,646ten%% %...% %...%1111%1111%1111%1111%1111%1111%1111%1101two%=%–3ten%1111%1111%1111%1111%1111%1111%1111%1110two%=%–2ten%1111%1111%1111%1111%1111%1111%1111%1111two%=%–1ten%2/8/11% Spring%2011%OO%Lecture%#7% 12%Sign%Bit%2/8/11%3%Peer%InstrucSon%QuesSon%• Suppose%we%had%a%5%bit%word.%What%integers%can%be%represented%in%two’s%complement?%%Red:%% %%O32%to%+31%%Orange:% %O31%to%+32%%Green:%% %0%to%+31%%Yellow:%% %O16%to%+15%%Pink:% % %O15%to%+15%%Blue:% % %O15%to%+16%2/8/11% Spring%2011%OO%Lecture%#7% 13%Peer%InstrucSon%Answer%• Suppose%we%had%a%5%bit%word.%What%integers%can%be%represented%in%two’s%complement?%%Red:%% %%O32%to%+31%%Orange:% %O31%to%+32%%Green:%% %0%to%+31%%Yellow:%% %M16%to%+15%%Pink:% % %O15%to%+15%%Blue:% % %O15%to%+16%2/8/11% Spring%2011%OO%Lecture%#7% 14%MIPS%Logical%InstrucSons%Logical operations C operators Java operators MIPS instructions Bit-by-bit AND & & and Bit-by-bit OR | | or Bit-by-bit NOT ~ ~ nor Shift left << << sll Shift right >> >>> srl 15%• %Useful%to%operate%on%fields%of%bits%within%a%word%%− %e.g.,%characters%within%a%word%(8%bits)%• %OperaSons%to%pack%/unpack%bits%into%words%• %Called%logical*opera(ons*Spring%2011%OO%Lecture%#7%2/8/11%BitObyOb it%D efiniSon%OperaCon%Input%Input%Output%AND%0%0%0%AND%0%1%0%AND%1%0%0%AND%1%1%1%OR%0%0%0%OR%0%1%1%OR%1%0%1%OR%1%1%1%NOR%0%0%1%NOR%0%1%0%NOR%1%0%0%NOR%1%1%0%2/8/11% Spring%2011%OO%Lecture%#7% 16%Examples%•
View Full Document