Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundation of Systems Xiang Lian The University of Texas-Pan American.

Similar presentations


Presentation on theme: "Foundation of Systems Xiang Lian The University of Texas-Pan American."— Presentation transcript:

1 Foundation of Systems Xiang Lian The University of Texas-Pan American

2 Digital Computers Only knows binary bits: 0 and 1 ( there are only 2 types of people in the world: those who understand binary, and those who don’t ) 29/13/2015

3 Binary to Decimal (x n x n-1 …x 0 ) 2 =x n ×2 n +x n-1 ×2 n-1 +…+x 0 ×2 0 X n : Most significant bit (MSB) X 0 : Least significant bit (LSB) Ex: (101) 2 =1×2 2 +0×2 1 +1×2 0 =4+0+1=5 9/13/20153

4 Decimal to Binary Ex: 11 binary number? 11/2: 1 (remainder) 5/2: 1 2/2: 0 1/2: 1 0 : STOP (1011) 2 9/13/20154

5 Binary Addition-Two Bits 9/13/20155 1 + 1 11 1 + 0 01 0 + 1 01 0 + 0 00 Carry bit Exercise: verify that they are correct in terms of decimal integers.

6 Binary Addition-Three Bits 9/13/20156 1 + 1 1 11 Exercise: verify that they are correct in terms of decimal integers. 1 + 1 0 10 1 + 0 1 10 1 + 0 0 01 0 + 1 1 10 0 + 1 0 01 0 + 0 1 01 0 + 0 0 00

7 Binary Addition: Bit-wise Addition 9/13/20157 1011 + 1111 ????? 101 1 + 111 1 1 ???? 0 Step 1: adding the rightmost bits, and recording the carry bit 10 1 1 + 11 1 1 1 1 ??? 1 0 Step 2: adding the next bits (to the left) with the carry bit, and recording the carry bit 1 0 11 + 1 1 1 1 11 ?? 0 10 Step 3: continue Step 2 until all bits are added. (finish it)

8 Exercise (Binary Addition) 9/13/20158 100111 + 110011 ???????

9 Finite Bits Computer use finite bits to represent integers. So only finite integers can be represented. Ex: 2 bits can only represent 0, 1, 2, 3. So overflow exists(finite bits are not sufficient) Ex: (10) 2 +(11) 2 =(110) 2 ? 9/13/20159

10 Finite Bits Computer use finite bits to represent integers. So only finite integers can be represented. Sln: use 8, 16, 32, 64, etc bits (integers needed in our life can be represented) So overflow exists(finite bits are not sufficient) What to do: depend on applications Who is responsible: systems, programmer, users. 9/13/201510

11 More Complicated: Negative Numbers -3 binary number? Sln: use extra bit for sign (sign bit), 0: + and 1: - Ex. (00) 2 =+0 (10) 2 =-0 (01) 2 =+1 (11) 2 =-1 9/13/201511 Problems 1.Two 0s (+0 and -0) 2.Not easy to add positive and negative numbers

12 Negative Numbers: 2’s Complement (x n x n-1 …x 0 ) 2 =-x n ×2 n +x n-1 ×2 n-1 +…+x 0 ×2 0 Ex: (00) 2 = =-0×2 1 +0×2 0 =0 (01) 2 = =-0×2 1 +1×2 0 =1 (10) 2 = =-1×2 1 +0×2 0 =-2 (11) 2 = =-1×2 1 +1×2 0 =-1 9/13/201512 Good: 1.only one zero 2.Easy to add negative and positive numbers 3.Two useful operations: negation and sign-extending Exercise: (11) 2 +(01) 2 =? (11) 2 +(10) 2 =? Note: X n not only represents sign, but has weights (different to the previous sign-magnititude representation).

13 2’s Complement: Negation (011) 2 =3 binary number of -3? (100) 2 : complement of (011) 2 +(001) 2 : plus one =(101) 2 : -3 9/13/201513 Exercise: find the negation of (110) 2 and (100) 2

14 2’s Complement: Sign Extension Extend (001) 2 to 6 bits ?????? (???001) 2 : copy the old bits to the right (000001) 2 : MSB  the remaining bits Exercise: extending (101) 2 to 6 bits and verify that they are the same integer. 9/13/201514

15 Exercise 1.Using 8 bits to represent integers. Add 100 and -55 Add -55 and -80 2.Find negation of 100 3.Sign extending -100 to 16 bits. 9/13/201515

16 Digital Computers Knows only binary bits (commands to control computers), which difficult for people to handle. Therefore, assembly languages were developed for people to control computers. In this textbook, we learn MIPS. 9/13/201516

17 MIPS An Instruction Set Architecture (ISA): a set of instructions(commands) 1.Provides interface for programmers 2.Different implementations may support the same ISA More see WIKI, MIPS Technology, Yahoo Finance.WIKIMIPS TechnologyYahoo Finance 9/13/201517

18 Instruction Normally requires 1.Operation (what to do: addition, and, jump, etc) 2.Operands (what to work on: register, memory, immediate numbers) Ex: add $s1, $s2, $s3: $s1=$s2+$s3 Operation: add Operands: $s1 (target), $s2 and $s3 (source) 9/13/201518

19 sub $s1, $s2, $s3: $s1=$s2-$s3 s Instruction Operands-Registers 9/13/201519 Processor 5-54 Registers$s1 $s2 $s3 × 9 After the execution of the sub instruction, $s1 holds 9 [=4-(-5)]

20 Instruction Operands-Memory Lw $s1, 20($s2): $s1  mem[20+$s2] 9/13/201520 Processor 5-54 $s1 $s2 $s3 Memory RegistersWord Address 2134 0113424 0× ? After the instruction, $s1 holds 1×2 16 +1×2 8 +34=65826

21 Instruction Operands-Immediate Number addi $s1, $s2, 20: $s1=$s2+20 9/13/201521 Processor 5-54 $s1 $s2 $s3Registers × 24 After the instruction, $s1 holds 4+20=24 Addi $s1, $s2, 20 Instruction registers

22 Instruction Operands-Questions 1.Why register operands Ans: for performance: registers are the fastest to access by CPU 2.Why memory operands Ans: for performance: only a few registers for which fast access is possible, then the other data must be in memory 3.Why immediate operands Ans: for performance: these numbers can be encoded in instruction, no need to access registers/memory 9/13/201522

23 Instruction Operations-Arithmetic Add $s1,$s2,$s3: $s1=$s2+$s3 Sub $s1,$s2,$s3: $s1=$s2-$s3 Addi $s1,$s2, 20: $s1=$s2+20 Exericse: For $s1=1,$s2=2,$s3=3, write the results after executing each instructions. Write an instruction so that $s1=$s1-1. 9/13/201523

24 Instruction Operations-Data Transfer Lw $s1, 30($s2): $s1  mem[$s2+30] (load/read word from memory) Sw $s1, 20($s2): $s1  mem[$s2+20] (store/write a word to memory) Lb $s1, 30($s2): $s1  mem[$s2+30] (load/read a byte from memory) Exercise: read a word/byte at $s2+20 in memory to $s2. 9/13/201524

25 Word vs. Byte Address 9/13/201525 Memory 2 00000000 Byte addressWord address 00000000 3 00000001 10 00000010 1 00000011 5 00000100 3 00000101 22 00000110 4 00000111 0 00001000 Aligment restriction: Word address must be a multiple of 4 (the last two bits are 00)

26 Word Value-Big/Little Endian 9/13/201526 Memory 2 Word address 00000000 3 10 1 5 00000100 3 22 4 0 00001000 Word value at address 00000000? big-endian: Value is : 2×2 24 +3×2 16 +10×2 8 +1=33753601 2 3 10 1 little-endian: Value is : 1×2 24 +10×2 16 +3×2 8 +2=17433346 1 10 3 2

27 Data Transfer-Exercise Suppose $s1 holds the address 0x00000000. Write instructions to swap the integers at address 0x00000000 and 0x00000100. Draw the memory contents after the swap. 9/13/201527 Memory 2 00000000 Byte address 3 00000001 10 00000010 1 00000011 5 00000100 3 00000101 22 00000110 4 00000111 0 00001000

28 Instruction Operation-Logical And – And $s1,$s2,$s3 Or – Or $s1,$s2,$s3 And immediate – Andi $s1,$s2,20 Shift left logic – Sll $s1, $s2,10 Etc. 9/13/201528

29 Logical Operation-And (Two Bits) The following table specifies And operations. 9/13/201529 Input 1Input 2Output of And 000 010 100 111

30 Logical Operation-And (Bit-Wise) 9/13/201530 1010 and 1110 ???? 1010 and 1110 ???0 For two binary numbers, and each bit independently (no carry bit like in addition) 1010 and 1110 ??10 Continue and finish the example

31 Logical Operation-Or (Two Bits) The following table specifies Or operations. 9/13/201531 Input 1Input 2Output of Or 000 011 101 111

32 Logical Operation-Or (Bit-Wise) 9/13/201532 1010 or 1110 ???? 1010 or 1110 ???0 For two binary numbers, or each bit independently (no carry bit like in addition) 1010 or 1110 ??10 Continue and finish the example

33 Logical Operation-Sll Sll $s1, $s1,3 9/13/201533 11110000111100001111000011111111 10000111100001111000011111111 111 000

34 Logical Operation-Exercise $s1=00000000 00000000 00000000 00001001 $s2=00000000 00000000 00000000 00001100 What is in $s1 after the following operation (independently) 1.Sll $s1, $s1,4 2.Srl $s1,$s1,4 3.And $s1,$s2,$s1 4.Or $s1,$s1,$s2 5.Andi $s1,$s2,8 9/13/201534

35 Instruction Operation-Conditional Branch Branch if equal – Beq $s1, $s2, 25 Branch if not equal – Bne $s1, $s2, 25 Set on less than – Slt $s1,$s2,$s3 Set on less than unsigned – Sltu $s1,$s2,$s3 Set on less than immediate – $slti $s1,$s2,20 9/13/201535

36 Conditional Branch-Beq Beq $s1, $s2, 25 9/13/201536 Processor 4-54 Memory Beq $s1, $s2, 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 128 24 Beq $s1,$s2,25 Instruction registers add $s1,$s2,$s3 28 Now pc=24, and $s1=$s2, then pc Changes to pc+4+25×4=128 24 pc

37 Conditional Branch-Beq (Cont.) Beq $s1, $s2, 25 9/13/201537 Processor 4-54 Memory Beq $s1, $s2, 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 128 24 Sub $s1,$s2,$s3 Instruction registers add $s1,$s2,$s3 28 Now pc=24, and $s1=$s2, then pc Changes to pc+4+25×4=128, Then next instruction to execute is from address 124. 128 pc

38 Conditional Branch-Bne Bne $s1, $s2, 25 9/13/201538 Processor 5-54 Memory Beq $s1, $s2, 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 128 24 Beq $s1,$s2,25 Instruction registers add $s1,$s2,$s3 28 Now pc=24, and $s1≠$s2, then pc Changes to pc+4+25×4=124 24 pc

39 Conditional Branch-Bne (Cont.) Beq $s1, $s2, 25 9/13/201539 Processor 4-54 Memory Beq $s1, $s2, 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 128 24 Sub $s1,$s2,$s3 Instruction registers add $s1,$s2,$s3 28 Now pc=24, and $s1≠$s2, then pc Changes to pc+4+25×4=128, Then next instruction to execute is from address 124. 128 pc

40 Conditional Branch-Slt Slt $s1, $s2, $s3 9/13/201540 Processor 4-54 $s1 $s2 Slt $s1,$s2,$s3 Instruction registers $s3 $s2!<$s3, so $s1=0. × 0 For this configuration and instruction slt $s1,$s3,$s2, however, $s1=1, since $s3 <$s2

41 Conditional Branch-Sltu Sltu $s1, $s2, $s3 9/13/201541 Processor 4-54 $s1 $s2 Slt $s1,$s2,$s3 Instruction registers $s3 $s2<$s3 when $s3 is an unsigned integer, so $s1=1. × 1 For this configuration and instruction slt $s1,$s3,$s2, however, $s1=0, since $s3 !<$s2

42 Conditiona Branch-Exercise Write an instruction for address 124 which jumps to the instruction at address 200 1.when $s1<$s2. 2.When $s1==$s2. 3.When $s1!=$s2 4.When $s1>$s2 9/13/201542 Memory Instruction 1 Sub $s1,$s2,$s3200 124 add $s1,$s2,$s3 128 Word Address

43 Instruction Operation-Unconditional Jump Jump register – Jr $ra Jump – J 2500 Jump-and-link instruction – Jal 2500 9/13/201543

44 Unconditional Jump-Jr Jr $ra 9/13/201544 Processor 5-54 Memory Jr $ra Sub $s1,$s2,$s3 $s1 $s2 Word Address 124 24 Jr $ra Instruction registers add $s1,$s2,$s3 28 Now $ra=124, then pc =$ra=124 124 $ra 24 pc

45 Unconditional Jump-Jr(Cont.) Jr $ra 9/13/201545 Processor 4-54 Memory Beq $s1, $s2, 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 124 24 Sub $s1,$s2,$s3 Instruction registers add $s1,$s2,$s3 28 Then next instruction to execute is from address 124. Now $ra=124, then pc =$ra=124 124 $ra 124 pc

46 Unconditional Jump-J J 25 9/13/201546 Processor 5-54 Memory J 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 100 24 J 25 Instruction registers add $s1,$s2,$s3 28 Now $pc=24 and the immediate number is 25, then pc =100 124 $ra 24 pc

47 Unconditional Jump-J (Cont.) J 25 9/13/201547 Processor 5-54 Memory J 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 100 24 Sub $s1,$s2,$s3 Instruction registers add $s1,$s2,$s3 28 124 $ra 100 pc Then next instruction to execute is from address 100. Now $pc=24 and the immediate number is 25, then pc =100

48 Addressing Mod of J In the previous example, Why is the target address 100 not 25? Because J use Pseudodirect addressing mode 9/13/201548 00000000 00000000 00000000 0001100000 00000000 00000000 00011001 00000000 00000000 00000000 01100100 Pc (32 bits)Immediate number (26 bits) Copy the leftmost 4 bits from pc, 26 bit from the immediate number, And then fill 00 in the last two bits Target address (32 bits)

49 Unconditional Jump-Jal Jal 25 9/13/201549 Processor 5-54 Memory Jal 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 100 24 Jal 25 Instruction registers add $s1,$s2,$s3 28 Now $pc=24 and the immediate number is 25, then $ra=pc+4=28 and pc =100 124 $ra 24 pc

50 Unconditional Jump-Jal(Cont.) Jal 25 9/13/201550 Processor 5-54 Memory Jal 25 Sub $s1,$s2,$s3 $s1 $s2 Word Address 100 24 Sub $s1,$s2,$s3 Instruction registers add $s1,$s2,$s3 28 Now $pc=24 and the immediate number is 25, then $ra=pc+4=28 and pc =100 28 $ra 100 pc

51 Unconditional Jump-Exercise Write an instruction for address 12 which 1.jump to the instruction at address 120. 2.jump to the instruction at address 120 and change $ra to 16. 9/13/201551 Memory Instruction 1 Sub $s1,$s2,$s3 Word Address 120 12 add $s1,$s2,$s3 16

52 Assembly Program.data L1:.word 1 20.text la $t0, L1 lw $t1,0($t0) add $t2,$zero,$t1 lw $t1,4($t0) add $t2,$t2,$t1 li $v0,10 #end program syscall 9/13/201552 An assembly program includes more than assembly instructions: directives, labels, comments, pseudo instructions, even procedure calls.

53 Program Loaded in Memory.data L1:.word 1 20.text la $t0, L1 lw $t1,0($t0) add $t2,$zero,$t1 lw $t1,4($t0) add $t2,$t2,$t1 li $v0,10 #end program syscall 9/13/201553 reserved syscall ori $2,$0,10 add $10,$10,$9 lw $9,4($8) add $10, $0,$9 lw $9, 0($8) lui $8, 4097 Text 1 Data 20 Dynamic data Stack 10010000 address

54 Details in the Assembly Program The.data and.text directives are used to separate variable declarations and assembly language instructions The.word directive is used to allocate and initialize space for a variable 9/13/2015Foundation of Systems, Yang Liu, UTPA54

55 Program with Branch/Jump 9/13/201555.data L1:.word 1 20 3 -5 L2:.word 0.text la $t0, L1 # load address of the target number la $t1, L2 # load address where termination should occur lw $t2,0($t0) #load the target number #search until either the target is found or all numbers are compared with the target LOOP: add $t0, $t0,4 # make 0($t0)$ point to next number beq $t1, $t0, EXIT # no more number to compare, exit lw $t3,0($t0) # load the number to compare beq $t2,$t3, EXIT # find a number equal to the target, exit j LOOP# continue search EXIT: li $v0,10 # terminate program syscall

56 Exericse Write a program with a positive integer n as input data such that the program output the summation of 1+…+n (output should be where the input data is). Ex. If n=3, output should be 1+2+3=6. And the program replace input 3 with output 6. 9/13/201556

57 Function Function is called by jal addr with extra work where addr is the starting address/label of the callee function 1.Changing stack pointers and saving registers which are used by the calling function and will be used by the callee function when entering the callee function. 2.When returning from the callee function, changing stack pointers and restore the saved registers. 3.The callee function calls jr $ra to return to the calling function 9/13/201557

58 Registers during Function Call Who save and restore registers: calling or callee function? Ans: depends on classification of registers (see the table on the right-bottom of the green page in the textbook) 9/13/201558

59 Exericse Write a program with a function which takes a positive integer n as an argument and calculates the summation of 1+…+n. The program has several input data to call the function, and output the sum for each input data Ex. If the program has 3 and 5 as input data, then output should be: 3 6 5 15 9/13/201559

60 Recursive Function A recursive function is a function which calls itself. 9/13/201560

61 Encoding Instructions 9/13/201561 FormatFieldsComments 6 bits5 bits 6 bits32 bits long R-formatOpRsRtRdShamtFunctarithmetic I-formatOpRsRtAddress/immediateTransfer,branch, immediate J-formatopTarget addressJump instruction Op: basic operation of the instruction Rs: the first register source operand Rt: the second register source operand Rd: The register destination operand Shamt: shift amount Funct: function code

62 Example Find the encoding of add $s0, $a1, $t7 1.Find the format of add: R-format from the green page. 2.Then find the encoding of all fields. 9/13/201562 FormatFieldsComments 6 bits5 bits 6 bits32 bits long R-format000000001010111110000????? (00000) 100000

63 Exercise Find the encoding of the following instructions. Addi $s1, $t1, -2 Bne $t0,$t0, -16 J 200 Sll $s1,$s1, 5 9/13/201563

64 Addressing Mode 1.Immediate: addi $s1, $s2, 5 2.Register addressing: add $s1,$s2,5; jr $ra 3.Base addressing: lw $s1, 4($s0) 4.PC-relative addressing: beq $s1,$s0, 5 5.Pseudodirect addressing: j 1024 9/13/201564


Download ppt "Foundation of Systems Xiang Lian The University of Texas-Pan American."

Similar presentations


Ads by Google