Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 312 Computer Architecture & Organization MIPS 3000 Assembly Programming Part 2 (Load, Store, data types and sys-calls) Department of Computer Science Southern Illinois University Edwardsville Fall, 2016 Dr. Hiroshi Fujinoki E-mail: [email protected] Assembly_Prog_01/001 CS 312 Computer Architecture & Organization Execution of a simple C/C++ statement by assembly instructions C/C++ statement Assembly instructions A = B + C; LW $S1, (address of B) LW $S2, (address of C) ADD $S3, $S2, $S1 Memory SW $S3, (address of A) Processor ALU (Arithmetic Logic Unit) B + C A Assembly_Prog_01/002 SW (Store Word) B C B+C S1 S2 S3 Processor Registers CS 312 Computer Architecture & Organization Different types of “LOAD” (and “STORE”) instructions li (load immediate) la (load address) lb (load byte) lw (load word) lhw (load half-word) Assembly_Prog_01/003 In the first programming assignment, we need to use these three “load” instructions CS 312 Computer Architecture & Organization li (load immediate) How does this As a 2’s complement = Load a constantprocessor (immediate) handleto a MIPS-3000 32-bit register Example this number? li $t0, 6 “Load Immediate” operator Don’t forget ‘,’ “Destination” register signed integer • A constant you want to the register “Source” parameter • Register name to which a constant will be placed What are the • Register name should have ‘$’ other 29 bits? MSB 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 LSB Assembly_Prog_01/004 CS 312 Computer Architecture & Organization la (load address) = Load a constant (immediate) as a 32-bit memory address Example • Memory address as a constant (immediate) (Using an immediate) la $s0, FA00CD0012 “Load Address” operator FA00CD0012 “la” does NOT load the contents of memory Assembly_Prog_01/005 “Destination” register “Source” parameter “la” just assigns a 32-bit Memoryaddress value to a register FA00CD0012 S0 register CS 312 Computer Architecture & Organization How “level”Only works? data (structure) or instructions are counted 4C000000 Your *.asm file “label” Memory # ################################ # Program Header # ################################ .data 4C000000 message1: .asciiz “Hello World!” message2: .asciiz “My name is …..” .text .globl main main: la $t4, message1 la $t1, message2 Assembly_Prog_01/006 Constant “4C0000000” will be loaded to t4 Constant “4C000000D” will be loaded to t1 H e l l o <space> W o r l d ! \0 M y 4C000000 4C000001 4C000002 4C000003 4C000004 4C000005 4C000006 4C000007 4C000008 4C000009 4C00000A 4C00000B 4C00000C 4C00000D 4C00000E CS 312 Computer Architecture & Organization la (load address) = Load a constant (immediate) as a 32-bit memory address Example • Memory address as a label (Using a label) la $s0, My_Address “Load Address” operator “Destination” register “Source” parameter Memory ***.asm 4C000000 My_Address: Label “My_Address” Assembly_Prog_01/007 Assembled My_Address: + Loaded XXXXXX CS 312 Computer Architecture & Organization la (load address) = Load a constant (immediate) as a 32-bit memory address Example • Memory address as a label (Using a label) la $s0, My_Address “Load Address” operator “Source” operand “Destination” operand Memory 0000CD0000 “la” does NOT load the contents of memory Assembly_Prog_01/008 “la” just assigns a 32-bit address value to a register 0000CD0000 S0 register CS 312 Computer Architecture & Organization lb (load byte) = Load a 8-bit data from memory to a MIPS-3000 32-bit register (This instruction will NOT modify the top 24 bits ) Example Four possible ways to specify Memory the target memory address • lb $t0, (FA00CD0012) • lb $t0, MY_LABEL 1-byte Target Address • lb $t0, ($t1) • lb $t0, 100 ($t1) t0 register MSB LSB Assembly_Prog_01/009 CS 312 Computer Architecture & Organization lb (load byte) = Load a 8-bit data from memory to a MIPS-3000 32-bit register (This instruction will NOT modify the top 24 bits ) Example Memory • lb $t0, (FA00CD0012) • lb $t0, MY_LABEL • lb $t0, ($t1) Target Address 1-byte 00011001 • lb $t0, 100 ($t1) t0 register 0 0 0 1 1 0 0 1 LSB MSB Assembly_Prog_01/010 Top 24 bits are preserved CS 312 Computer Architecture Organization The & target address MUST be a multiple of 4! lw (load word) = Load a 32-bit data from memory to a MIPS-3000 32-bit register lw $t0, FFFFFFE Example Memory • lw $t0, (FA00CD0012) • lw $t0, MY_LABEL • lw $t0, ($t1) Target Address 1-byte Illegal address! 4 bytes • lw $t0, 100 ($t1) MSB LSB Assembly_Prog_01/011 t0 register CS 312 Computer Architecture & Organization What’s the difference? • li $t0, 6 = FFFFFFFF (for a 32-bit processor) Can it be a negative number? -(2(N-1)) • la $s0, FA00CD0012 (2(N-1)) -1 0 NOT a memory access 2N -1 • lw $t0, (FA00CD0012) • lw $t0, FA00CD0012 Target Address Is this an illegal parameter (operand)? Assembly_Prog_01/012 Memory A memory access CS 312 Computer Architecture & Organization System Calls • Systems calls in SPIM is something like sub-routine calls in a high-level programming language that perform useful tasks • Such as: - Print a message (a character string) on the local monitor - Print an integer on the local monitor - Take a user input from the keyboard and store the key input in a register • A list of SPIM system calls available Assembly_Prog_01/013 Figure A.17 (p. A-49) CS 312 Computer Architecture & Organization Major system calls available in MIPS R3000 simulator System Call Service Call Code (in $v0) print an integer 1 print a floating-point number 2 print a double-precision fp# 3 print a char string 4 read an integer from k.b. 5 read a floating-point # from k.b. 6 read a double-precision # from k.b. 7 read a character string from k.b. 8 Arguments $a0 = integer $f12 = float $f12 = double fp $a0 = string address Result integer in $v0 float # in $v0 double # in $v0 $a0 = buffer address, $a1 = length The system calls with the yellow background Those we use in the first programming assignment Assembly_Prog_01/014 CS 312 Computer Architecture & Organization System Call: Print a message (a character string on the local monitor) Procedure for SPIM system-call #4: “print a message” Specify the type of your message data Declare the beginning of the data section System Call #4 = “print a message” .data Create a label for your character string my_string: .asciiz “Hello World!” .text .globl main Your message as an ASCII character string main: li $v0, 4 la $a0, my_string syscall jr $31 Assembly_Prog_01/015 System call Memory address where your message is stored CS 312 Computer Architecture & Organization System Call: Print a number (integer) on the local monitor Nothing to be prepared in .data Procedure for SPIM system-call #1: “print a number” .data It’s “li” (NOT “la”) System Call #1 = “print a number” .text .globl main main: li $v0, 1 li $a0, 9 syscall jr $31 The number you want to display as an immediate Assembly_Prog_01/016 CS 312 Computer Architecture & Organization System Call: Take a user input (as an integer) from the keyboard Nothing to be prepared in .data Procedure for SPIM system-call #5: “take a user input (integer)” .data System Call #5 = “Take user input” .text .globl main main: li $v0, 5 syscall jr $31 Assembly_Prog_01/017 The input value goes to $v0 register CS 312 Computer Architecture & Organization Conditional branches and jumps • Immediate address • Label Conditional branches • beq $t0, $t1, <destination address> Compare $t0 and $t1 registers. If they hold the same value jump to the instruction in the destination address. • bne $t0, $t1, <destination address> Compare $t0 and $t1 registers. If they hold the different value, jump to the instruction in the destination address. Jump (= unconditional branch) • j <destination address> Always jump to the instruction in the destination address. Assembly_Prog_01/018 CS 312 Computer Architecture & Organization Examples for conditional branches .text .globl main jump is needed to skip “if_equal” main: These instructions will be executed only if t0 t1! beq $t0, $t1, if_equal instruction A This implements if-then-else structure j end_if_else if_equal: instruction B end_if_else: instruction C Assembly_Prog_01/019 These instructions will be executed only if t0 = t1! CS 312 Computer Architecture & Organization Copying a register to another • Load instructions can not be used for data transfer between two registers • Move instructions must be used for register-to-register data transfer Example move $s0, $s1 “To” “From” Simple integer arithmetic • add $t0, $t1, $t2 (t0 = t1 + t2) • sub $t0, $t1, $t2 (t0 = t1 - t2) Assembly_Prog_01/020 CS 312 Computer Architecture & Organization Helpful Resources The list of available registers (B-24) Conditional branch instructions (B-59 through B-63) beq, bgez, blez, bne, ble, etc. The list of available system-calls (B-44) Sample programs for the major program-flow control (pp. 105-111) Assembly_Prog_01/021 CS 312 Computer Architecture Organization The & target address MUST be a multiple of 2! lhw (load half-word) = Load a 16-bit data from memory to a MIPS-3000 32-bit register (This instruction will NOT modify the top 16 bits ) lw $t0, FFFFFFF Example Memory • lhw $t0, (FA00CD0012) • lhw $t0, MY_LABEL • lhw $t0, ($t1) Target Address 1-byte Illegal address! 2 bytes • lhw $t0, 100 ($t1) t0 register MSB LSB Top 16 bits are preserved Assembly_Prog_01/000