1. Trang chủ
  2. » Giáo án - Bài giảng

kiến trúc máy tính võ tần phương chương ter02 2mips assignmentignmentignmentignmentembly sinhvienzone com

65 45 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 65
Dung lượng 0,96 MB

Nội dung

dce 2013 COMPUTER ARCHITECTURE CE2013 BK TP.HCM Faculty of Computer Science and Engineering Department of Computer Engineering Vo Tan Phuong http://www.cse.hcmut.edu.vn/~vtphuong CuuDuongThanCong.com https://fb.com/tailieudientucntt dce 2013 Chapter MIPS Instruction Set Architecture CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Presentation Outline • Assembly Language Statements • Assembly Language Program Template • Defining Data • Memory Alignment and Byte Ordering • System Calls • Procedures • Parameter Passing and the Runtime Stack CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Assembly Language Statements • Three types of statements in assembly language – Typically, one statement should appear on a line Executable Instructions – Generate machine code for the processor to execute at runtime – Instructions tell the processor what to Pseudo-Instructions and Macros – Translated by the assembler into real instructions – Simplify the programmer task Assembler Directives – Provide information to the assembler while translating a program – Used to define segments, allocate memory variables, etc – Non-executable: directives are not part of the instruction set CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Instructions • Assembly language instructions have the format: [label:] mnemonic [operands] [#comment] • Label: (optional) – Marks the address of a memory location, must have a colon – Typically appear in data and text segments • Mnemonic – Identifies the operation (e.g add, sub, etc.) • Operands – Specify the data required by the operation – Operands can be registers, memory variables, or constants – Most instructions have three operands L1: addiu $t0, $t0, CuuDuongThanCong.com Computer Architecture – Chapter 2.2 #increment $t0 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Comments • Comments are very important! – Explain the program's purpose – When it was written, revised, and by whom – Explain data used in the program, input, and output – Explain instruction sequences and algorithms used – Comments are also required at the beginning of every procedure • Indicate input parameters and results of a procedure • Describe what the procedure does • Single-line comment – Begins with a hash symbol # and terminates at end of line CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Next • Assembly Language Statements • Assembly Language Program Template • Defining Data • Memory Alignment and Byte Ordering • System Calls • Procedures • Parameter Passing and the Runtime Stack CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Program Template # Title: Filename: # Author: Date: # Description: # Input: # Output: ################# Data segment ##################### data ################# Code segment ##################### text globl main main: # main program entry li $v0, 10 # Exit program syscall CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 DATA, TEXT, & GLOBL Directives • DATA directive – Defines the data segment of a program containing data – The program's variables should be defined under this directive – Assembler will allocate and initialize the storage of variables • TEXT directive – Defines the code segment of a program containing instructions • GLOBL directive – Declares a symbol as global – Global symbols can be referenced from other files – We use this directive to declare main procedure of a program CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS dce 2013 Layout of a Program in Memory 0x7FFFFFFF Stack Segment Stack Grows Downwards Memory Addresses in Hex Dynamic Area Data Segment 0x10000000 Static Area Text Segment 0x04000000 Reserved CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 10 dce 2013 Nested Procedures Outcome caller: jal rt_1 next: rt_1: to_2: bne add jr addi jal jr $a0, $zero, to_2 $v0, $zero, $zero $ra $a0, $a0, -1 rt_2 $ra rt_2: • On the call to rt_1, the return address (next in the caller routine) gets stored in $ra What happens to the value in $ra (when i != 0) when rt_1 makes a call to rt_2? CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 51 dce 2013 Saving the Return Address, Part • Nested procedures (i passed in $a0, return value in $v0) high addr  $sp old TOS low addr rt_1: bne add jr to_2: addi sw sw addi jal bk_2: lw lw addi jr $a0, $v0, $ra $sp, $ra, $a0, $a0, rt_2 $a0, $ra, $sp, $ra $zero, to_2 $zero, $zero $sp, -8 4($sp) 0($sp) $a0, -1 0($sp) 4($sp) $sp, $ra • Save the return address (and arguments) on the stack CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 52 dce 2013 Saving the Return Address, Part • Nested procedures (i passed in $a0, return value in $v0) high addr rt_1: $sp old TOS to_2: bk_2: low addr $ra bne add jr addi sw sw addi jal lw lw addi jr $a0, $v0, $ra $sp, $ra, $a0, $a0, rt_2 $a0, $ra, $sp, $ra $zero, to_2 $zero, $zero $sp, -8 4($sp) 0($sp) $a0, -1 0($sp) 4($sp) $sp, • Save the return address (and arguments) on the stack CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 53 dce 2013 Preserved/Not preserved Registers CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 54 dce 2013 Non-Leaf Procedure Example • C code: int fact (int n) { if (n < 1) return 1; else return n * fact(n - 1); } – Argument n in $a0 – Result in $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 55 dce 2013 Non-Leaf Procedure Example (cont) • MIPS code: fact: addi sw sw slti beq addi addi jr L1: addi jal lw lw addi mul jr $sp, $ra, $a0, $t0, $t0, $v0, $sp, $ra $a0, fact $a0, $ra, $sp, $v0, $ra CuuDuongThanCong.com $sp, -8 4($sp) 0($sp) $a0, $zero, L1 $zero, $sp, $a0, -1 0($sp) 4($sp) $sp, $a0, $v0 Computer Architecture – Chapter 2.2 # # # # adjust stack for items save return address save argument test for n < # # # # # # # # # # if so, result is pop items from stack and return else decrement n recursive call restore original n and return address pop items from stack multiply to get result and return https://fb.com/tailieudientucntt © Fall 2013, CS 56 dce 2013 Compiling a Recursive Procedure • A procedure for calculating factorial int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } • A recursive procedure (one that calls itself!) fact (0) = fact (1) = * = fact (2) = * * = fact (3) = * * * = fact (4) = * * * * = 24 • Assume n is passed in $a0; result returned in $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 57 dce 2013 Compiling a Recursive Procedure fact: addi sw sw slti beq addi addi jr L1: $sp, $ra, $a0, $t0, $t0, $v0, $sp, $ra $sp, -8 #adjust stack pointer 4($sp) #save return address 0($sp) #save argument n $a0, #test for n < $zero, L1 #if n >=1, go to L1 $zero, #else return in $v0 $sp, #adjust stack pointer #return to caller (1st) addi $a0, $a0, -1 #n >=1, so decrement n jal fact #call fact with (n-1) #this is where fact returns bk_f: lw lw addi mul jr CuuDuongThanCong.com $a0, $ra, $sp, $v0, $ra 0($sp) #restore argument n 4($sp) #restore return address $sp, #adjust stack pointer $a0, $v0 #$v0 = n * fact(n-1) #return to caller (2nd) Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 58 dce 2013 A Look at the Stack for $a0=2 – old TOS  $sp $ra $a0 • Stack state after execution of the first encounter with jal (second call to fact routine with $a0 now holding 1) − saved return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack − saved original value of $a0 on the stack $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 59 dce 2013 A Look at the Stack for $a0=2 – old TOS $sp $ra • Stack state after execution of the second encounter with jal (third call to fact routine with $a0 now holding 0) − saved return address of instruction in caller routine (instruction after jal) on the stack − saved previous value of $a0 on the stack $a0 $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 60 dce 2013 A Look at the Stack for $a0=2 – old TOS $sp • Stack state after execution of the first encounter with the first jr ($v0 initialized to 1) − stack pointer updated to point to third call to fact $ra $a0 $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 61 dce 2013 A Look at the Stack for $a0=2 – old TOS $sp $ra $a0 • Stack state after execution of the first encounter with the second jr (return from fact routine after updating $v0 to * 1) − return address to caller routine (bk_f in fact routine) restored to $ra from the stack − previous value of $a0 restored from the stack − stack pointer updated to point to second call to fact $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 https://fb.com/tailieudientucntt © Fall 2013, CS 62 dce 2013 A Look at the Stack for $a0=2 – old TOS $sp $ra $a0 $v0 CuuDuongThanCong.com Computer Architecture – Chapter 2.2 • Stack state after execution of the second encounter with the second jr (return from fact routine after updating $v0 to * * 1) − return address to caller routine (main routine) restored to $ra from the stack − original value of $a0 restored from the stack − stack pointer updated to point to first call to fact https://fb.com/tailieudientucntt © Fall 2013, CS 63 dce 2013 Aside: Allocating Space on the Stack high addr Saved argument regs (if any) Saved return addr • The segment of the stack containing a procedure’s saved $fp registers and local variables is its procedure frame (aka activation record) – The frame pointer ($fp) points to the first word of the frame of a procedure – providing a stable “base” register for the procedure Saved local regs (if any) Local arrays & structures (if any) $sp low addr CuuDuongThanCong.com Computer Architecture – Chapter 2.2 • $fp is initialized using $sp on a call and $sp is restored using $fp on a return https://fb.com/tailieudientucntt © Fall 2013, CS 64 dce 2013 Aside: Allocating Space on the Heap • Static data segment for constants and other static variables (e.g., arrays) $sp Memory 0x 7fff fffc Stack – $gp initialized to address allowing ± offsets into this segment • Dynamic data segment (aka heap) for structures $gp that grow and shrink (e.g., linked lists) – Allocate space on the heap with malloc() and free it with free() in C CuuDuongThanCong.com Computer Architecture – Chapter 2.2 PC Dynamic data (heap) 0x Static data 0x 1000 8000 1000 0000 Text (Your code) 0x 0040 0000 Reserved 0x 0000 0000 https://fb.com/tailieudientucntt © Fall 2013, CS 65 ... addiu $t0, $t0, CuuDuongThanCong .com Computer Architecture – Chapter 2.2 #increment $t0 https://fb .com/ tailieudientucntt © Fall 2013, CS dce 2013 Comments • Comments are very important! – Explain... initializer] var1: WORD 10 • All initializers become binary data in memory CuuDuongThanCong .com Computer Architecture – Chapter 2.2 https://fb .com/ tailieudientucntt © Fall 2013, CS 12 dce 2013... CuuDuongThanCong .com Computer Architecture – Chapter 2.2 https://fb .com/ tailieudientucntt © Fall 2013, CS 17 dce 2013 Symbol Table • Assembler builds a symbol table for labels (variables) – Assembler computes

Ngày đăng: 28/01/2020, 23:10