Download
assembly language n.
Skip this Video
Loading SlideShow in 5 Seconds..
Assembly Language PowerPoint Presentation
Download Presentation
Assembly Language

Assembly Language

21 Views Download Presentation
Download Presentation

Assembly Language

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Assembly Language Chapter 3

  2. Computer Languages • High-level languages • Java, Python, C, ASP, HTML, … • Low-level languages • Assembly language: symbolic representation of machine instructions. • Assembler: a compiler which translates from an assembly languageto a machine language. • Machine languages

  3. Components of a computer: More detail processor memory registers ALU data program Instruction register Program counter data address Control units

  4. Applications Operating systems Instruction set Functional units Finite state machine Logic gates Electronics Levels of descriptions of computer systems • computer architecture begins at the instruction set. • An instruction set is what a programmer at the lowest level sees of a processor • one instruction set with different level of performance for many models, based on the implementation of a control unit via “microprogram”. • Present day processor designs converge, their instruction sets become more similar than different.

  5. Registers NameReg. No. Usage $zero 0 hardwired 0 $v0-$v1 2-3 return value and expression evaluation $a0-$a3 4-7 arguments $t0-$t7 8-15 temporary values $s0-$s7 16-23 saved values $t8-$t9 24-25 more temporary values $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address C

  6. MIPS operands

  7. MIPS Assembly Instructions: Arithmetic

  8. MIPS Assembly Instructions: Data Transfer

  9. MIPS Assembly Instructions: Logical C

  10. MIPS Assembly Instructions

  11. MIPS Assembly Instructions: Conditionals Chapter 3 Assembly Languages

  12. x = y+z+5; w = z-y; lw $t0, 18($zero) # load y lw $t1, 22($zero) # load z add $t2, $t0, $t1 # y+z addi $t2, $t2, 5 # (y+z)+5 sw $t2, 14($zero) # store x sub $t3, $t1, $t2 # z-y sw $t3, 10($zero) # store w if (a<b) a++; else b++; lw $t0, 50($zero) # load a lw $t1, 54($zero) # load b slt $t2, $t0, $t1 # a<b beq $t2, $zero, else # if addi $t0, $t0, 1 # a++ sw $t0, 50($zero) # store a else: addi $t1, $t1, 1 # b++ sw $t1, 54($zero) # store b Example 10 14 18 22 50 54 Chapter 3 Assembly Languages

  13. Basic instruction formats R- format I- format J- format Chapter 3 Assembly Languages

  14. Instruction Representation: Examples Chapter 3 Assembly Languages

  15. Instruction Representation: Examples add $t0, $t1, $t2 Chapter 3 Assembly Languages

  16. Instruction Representation: Examples sub $s0, $s1, $s2 Chapter 3 Assembly Languages

  17. Instruction Representation: Examples addi $s0, $s1, 100 Chapter 3 Assembly Languages

  18. Instruction Representation: Examples j 10000 Chapter 3 Assembly Languages

  19. Instruction Representation: Examples beq $s0, $s1, 100 Chapter 3 Assembly Languages

  20. MIPS Simulator http://pages.cs.wisc.edu/~larus/spim.html Chapter 3 Assembly Languages

  21. Case/ Switch statement switch (i) { case 0: i++; break; case 1: j++; break; case 4: k++; } • Jump (address) table • Table storing addresses of different cases Chapter 3 Assembly Languages

  22. switch (i) { case 0: i++; break; case 1: j++; break; case 4: k++; } # variable i is in $t1, const 5 is in $t0 # start addr of jump table (e.g. 3010) is in $s0 blt $t1, $zero, out bge$t1, $t0, out # multiply i by 4 for word size sll $t1, $t1, 2 # find entry in the jump table add $t1, $t1, $s0 # load addr in jump table to $t2 lw $t2, 0($t1) jr $t2 L2010: … j out L20485: … out: Case statement and jrinstruction Chapter 3 Assembly Languages

  23. Procedure calls • Steps to Execute procedure • place parameters in a place where procedure can access them • transfer control to the procedure • acquire the storage needed for the procedure • perform the desired task • place the result value in a place where the calling program can access it • return to the control point of origin Chapter 3 Assembly Languages

  24. Memory allocation for programs $sp Stack Heap (dynamic data) Static data Code (text segment) Reserved $fp $gp pc Chapter 3 Assembly Languages

  25. Registers • $a0-$a3 • four argument registers in which to pass parameters • $v0-$v1 • two value registers in which to return values • $ra • one return address register to return to the point of origin Chapter 3 Assembly Languages

  26. jal jump and link • jumps to an address and simultaneously saves the address of the following instruction in register $ra • jal ProcedureAddress • jal puts PC + 4 in $ra • to return, after completion of the procedure: • jr $ra • calling program (caller) puts parameter values in $a0-$a3 • callee performs calculations and places result in $v0-$v1 Chapter 3 Assembly Languages

  27. Call sequence • Put arguments in $a0-$a3 • The rest of the arguments (if exist) are placed in the frame or activation record • Allocate a frame in stack (update $sp) • Save values of $s0-$s7, $fp, $rain the frame • Update $fp • Execute jal instruction Chapter 3 Assembly Languages

  28. Return sequence • Put the return value in $v0 • Restore registers $s0-$s7 • Restore $fp • Pop the frame from stack • Execute jr $ra Chapter 3 Assembly Languages

  29. int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } leaf example: addi $sp, $sp, -12 #adjust stack to make room for 3 items sw $t1, 8($sp) #save register $t1 for use afterwards sw $t0, 4($sp) #save register $t0 for use afterwards sw $s0, 0($sp) #save register $s0 for use afterwards add $t0, $a0, $a1 #register $t0 contains g + h add $t1, $a2, $a3 #register $t1 contains i + j sub $s0, $t0, $t1 #f = $t0 - $t1, which is (g+h)-(i+j) add $v0, $s0, $zero #returns f ($v0 = $s0 + 0) lw $s0, 0($sp) #restore register $s0 for caller lw $t0, 4($sp) #restore register $t0 for caller lw $t1, 8($sp) #restore register $t1 for caller addi $sp, $sp, 12 #adjust stack to delete 3 items jr $ra #jump back to calling routing Example 1 Chapter 3 Assembly Languages

  30. int fact (int n) { if (n<2) return(1); else return(n*fact(n-1); } fact: addi $sp, $sp, -8 #adjust stack to make room for 2 items sw $ra, 4($sp) #save the return address sw $a0, 0($sp) #save the argument n slti $t0, $a0, 2 #test for n<2 beq $t0, $zero, L1 #if n>=2 goto L1 addi $sp, $sp, 8 #adjust stack to delete 2 items addi $v0, $zero, 1 #else return 1 jr $ra #jump back to calling routing L1: addi $a0, $a0, -1 #if n>=1 calculate n-1 jal fact #call fact with n-1 lw $a0, 0($sp) #return from jal, restore argument lw $ra, 4($sp) #restore return address addi $sp, $sp, 8 #adjust stack to delete 2 items mul $v0, $a0, $v0 # return n* fact(n-1) jr $ra #jump back to calling routing Example 2 Chapter 3 Assembly Languages

  31. fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 2 beq $t0, $zero, L1 addi $sp, $sp, 8 addi $v0, $zero, 1 jr $ra L1: addi $a0, $a0, -1 jal fact lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 mul $v0, $a0, $v0 jr $ra int fact (int n) { if (n<2) return(1); else return(n*fact(n-1); } … fact(2); Execution example Push frame Call fact (2)addi $a0, $zero, 2 (03A0)jal fact $a0 2 n<2 ret 1 1 stack 03A4 ($ra) 2($a0) $ra 03A4 0344 (0340) 0344 ($ra) 1($a0) restore $v0 1 2 n>=2 ret n*(n-1)! Chapter 3 Assembly Languages

  32. 32-bit constants • A register can contain an integer between -2147483648 (-231) and 2147483647 • Immediate data can be between -32768 (-215) and 32767 • addi $t0, $zero, 32767 • How can we use 32-bit constant? • lui (load upper immediate) instruction Chapter 3 Assembly Languages

  33. load upper immediate instruction: lui • Store 16-bit immediate data in the upper half of the register lui $t0, 16384# put 0100 0000 0000 0000 in the upper half of $t0 • To store a 32-bit constant in a register add $t0, $zero, $zero lui $t0, 16384 addi $t0, $t0, 5435 0100 0000 0000 0000 Chapter 3 Assembly Languages

  34. Jumping to far-away instruction • In beq or bne instructions, only  215 distance can be specified. • To jump to further instructions beq $t0, $t1, L1 ... L1: j L2 ... L2: Chapter 3 Assembly Languages

  35. Memory Addressing Modes • direct • mem[address] • register indirect • mem[content[reg]] • mem[content[address]] • register indirect displacement • mem[content[reg]+displacement] • mem[content[address]+displacement] • register indirect index and displacement Chapter 3 Assembly Languages

  36. MIPS Addressing Modes • Register addressing (R-format) • add $t0,$t1,$t2 • Base (Displacement) addressing (I-format) • lw $t1, 12($t2) • Immediate addressing (J-format) • addi $t0, $t1, 34 register memory + register Chapter 3 Assembly Languages

  37. MIPS Addressing Modes • PC-relative addressing (I-format) • beq $t0, $t1, label • Pseudo-direct addressing • j label memory + PC : PC Chapter 3 Assembly Languages