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