do while example
play

Do-While Example In C++ do { z--; while (a == b); z = b; In - PowerPoint PPT Presentation

Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than ( slt ) compares its source


  1. Do-While Example • In C++  do {  z--;  while (a == b);  z = b; • In assembly language  loop: addi $s2, $s2, -1  beq $s0, $s1, loop  or $s2, $s1, $zero 25

  2. Comparisons • Set on less than ( slt ) compares its source registers and sets its destination register to 1 if src1 < src2 and to 0 otherwise (branch-if-less-than)  Syntax: slt dest , src1 , src2 • Set on less than immediate ( slti ) perform the same comparison, but its second source operation is an immediate value  Syntax: slti dest , src1 , immed • Combined with beq and bne these instructions can implement all possible relational operators 26

  3. Relational Branches (1) • Branch if less than  slt $t0, src1 , src2  bne $zero, $t0, label • Branch if greater than or equal  slt $t0, src1 , src2  beq $zero, $t0, label 27

  4. Relational Branches (2) • Branch if greater than  slt $t0, src2 , src1  bne $zero, $t0, label • Branch if less than or equal  slt $t0, src2 , src1  beq $zero, $t0, label 28

  5. Case/Switch Statement * • Can be implemented like a chain of if-then- else statements • Using a jump address table is faster • Must be able to jump to an address loaded from memory • Jump register ( jr ) gives us that ability • Syntax: jr src  Instruction: jr $t1 #go to address in $t1 29

  6. Compiling a Case (Switch) Statement Memory switch (k) { case 0: h=i+j; break; /*k=0*/ L2 case 1: h=i+h; break; /*k=1*/ L1 case 2: h=i-j; break; /*k=2*/ $t4 → L0 • Assuming three sequential words in memory starting at the address in $t4 have the addresses of the labels L0, L1, and L2 and k is in $s2 add $t1, $s2, $s2 #$t1 = 2*k add $t1, $t1, $t1 #$t1 = 4*k add $t1, $t1, $t4 #$t1 = addr of JumpT[k] lw $t0, 0($t1) #$t0 = JumpT[k] jr $t0 #jump based on $t0 L0: add $s3, $s0, $s1 #k=0 so h=i+j j Exit L1: add $s3, $s0, $s3 #k=1 so h=i+h j Exit L2: sub $s3, $s0, $s1 #k=2 so h=i-j Exit: . . . 30

  7. MIPS Organization Processor Memory Register File 1…1100 src1 addr src1 5 data 32 src2 addr 32 5 registers dst addr read/write ($zero - $ra) src2 5 addr write data data 2 30 32 32 32 words 32 bits br offset read data 32 Add PC 32 32 32 32 Add 32 4 write data 0…1100 Fetch 32 0…1000 PC = PC+4 32 4 5 6 7 0…0100 32 ALU 0 1 2 3 0…0000 Exec Decode 32 word address 32 bits 32 (binary) byte address (big Endian) 31

  8. Variable Index into Array • Previous method for accessing array elements will only work if index is constant • If index is a variable we must compute the address in code • Formula: address + (index × width) • Since width is usually a power of two, we can use a left shift to perform the multiplication 32

  9. Structures • Like an array, a structure (struct) is made up of several smaller data elements stored contiguously in memory • However the elements do not have to all be the same type • The compiler will construct a table mapping each structure member to an offset 33

  10. Bit Fields • Structure members can be bit fields • In this case multiple members are packed into a single byte or word • To retrieve the value of a bit field, AND the word with a mask and then shift right • To set the value of a bit field use a left shift and an OR 34

  11. Procedures * • A function (or procedure) is a sequence of instructions that can be used to perform a task • When a function is called control transfers to the function; once the function completes its task it returns • Functions may accept parameters (arguments) and/or return a value • A function can also define local variables for its use which exist only until the function returns • Functions can call other functions 35

  12. Call and Return • Call and return are nothing more than unconditional jumps • The calling function (the caller ) jumps to the beginning of the function • The called function (the callee ) jumps back to the point from which it was called ( return address ) • Since a function can be called from any number of places the return address changes with each call 36

  13. Jump and Link • The jump and link ( jal ) instruction performs an unconditional jump just like j , but first saves the address of the next instruction in the return address register ( $ra )  jal label • A function returns by jumping to the address stored in that register:  jr $ra 37

  14. Example (1) • Calling a function:  jal function1 • Function:  function1: add $t0, $t1, $t2  sub $t3, $t4, $t5  jr $ra 38

  15. Calling Conventions • We need a way to pass parameters to a function and get a return value from it • A calling convention is a set of rules that define how parameters and return values are passed to/from functions as well as which registers a function can use • Allows us to call functions compiled by different compilers and even languages 39

  16. Parameters/Return Value • Most MIPS software uses the same calling convention • The first four parameters are placed in registers ($a0-$a3) additional parameters are placed on the stack • Return values go in registers $v0-$v1 40

  17. MIPS Registers Name Number Usage Saved on Call? $zero 0 N/A Zero (hardware) $at 1 No reserved Assembler Temp $v0-$v1 2-3 Return Value/Temporaries No $a0-$a3 4-7 Arguments No $t0-$t9 8-15, 24-25 Temporaries No $s0-$s7 16-23 Saved Values Yes $k0-$k1 26-27 Reserved for OS Kernel No $gp 28 Global Pointer Yes $sp 29 Stack Pointer Yes $fp 30 Frame Pointer Yes $ra 31 Yes Return Address (hardware) 41

  18. Example (2) • Function call  or $a0, $s0, $zero  jal function2  or $s0, $v0, $zero • Function definition  function2: add $v0, $a0, $a0  jr $ra 42

  19. Register Use • Callee-saved registers are owned by caller; if a function uses a callee-saved register it must save the old value and restore that value before it returns • Caller-saved registers are owned by the callee; if a caller is using a caller-saved register, it must save the old value before calling the function and restore it after the function returns • In MIPS, $s0-$s7 are callee-saved and $t0-$t9 are caller-saved 43

  20. The Stack • Data that a function needs to save in memory is pushed onto the stack • Before returning, the function pops the data off of the stack • In MIPS, the stack pointer ($sp) contains the address of the last word pushed onto the stack • The stack grows downward from higher addresses to lower ones 44

  21. Spilling Registers • What if the callee needs more registers? What if the procedure is recursive?  uses a stack – a last-in-first-out queue – in memory for passing additional values or saving (recursive) return address(es)  One of the general registers, $sp, is used to address the stack (which “grows” from high address to low high addr address)  add data onto the stack – push top of stack $sp $sp = $sp – 4 data on stack at new $sp  remove data from the stack – pop data from stack at $sp low addr $sp = $sp + 4 45

  22. Accessing the Stack • Push a word onto the stack  addi $sp, $sp, -4  sw src , 0($sp) • Pop a word off of the stack  lw dest , 0($sp)  addi $sp, $sp, 4 • Typically we batch multiple pushes and pops together 46

  23. Accessing the Stack (cont'd) • Push three words onto the stack  addi $sp, $sp, -12  sw $t0, 8($sp)  sw $t1, 4($sp)  sw $s0, 0($sp) • Pop three words off of the stack  lw $s0, 0($sp)  lw $t1, 4($sp)  lw $t0, 8($sp)  addi $sp, $sp, 12 47

  24. Stack Illustration High address $sp $sp Contents of register $t1 Contents of register $t0 $sp Contents of register $s0 Low address a. b. c. 48

  25. Data on the Stack • Data a function puts on the stack makes up its stack frame or activation record • If there are more than four parameters, the extra parameters must be pushed onto the stack • Non-leaf functions must save the value of the return address register ($ra) 49

  26. Data on the Stack (cont'd) • Any registers that must be saved are stored on the stack • Also local variables that won't fit into registers (either because there are no registers left or the data is too big) • Commonly a frame pointer ($fp) is used in addition to the stack pointer, since the stack pointer could change during the function 50

  27. Stack Frame High address $fp $fp $sp $sp $fp Saved argument registers (if any) Saved return address Saved saved registers (if any) Local arrays and structures (if any) $sp Low address b. c. a. 51

  28. Where Data is Stored • The static data area holds global variables  Fixed size  In MIPS, the global pointer ($gp) contains the address of this area • The stack holds local variables and function- related information  Dynamic and managed automatically by the compiler • The heap holds data allocated through new or malloc  Dynamic and managed by the programmer 52

  29. Program Memory $sp 7fff ffff Stack hex Dynamic data $gp 1000 8000 Static data hex 1000 0000 hex Text pc 0040 0000 hex Reserved 0 53

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend