We will study the MIPS assembly language as an exemplar of the concept.

Similar documents
MIPS Hello World. MIPS Assembly 1. # PROGRAM: Hello, World! # Data declaration section. out_string:.asciiz "\nhello, World!\n"

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Chapter 2. Instructions:

EEC 581 Computer Architecture Lecture 1 Review MIPS

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Chapter 3. Instructions:

Chapter 2. Instruction Set Architecture (ISA)

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Chapter 2A Instructions: Language of the Computer

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CS 61c: Great Ideas in Computer Architecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

CS 61c: Great Ideas in Computer Architecture

ECE 30 Introduction to Computer Engineering

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

MIPS (SPIM) Assembler Syntax

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

MIPS Reference Guide

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

MIPS Assembly Language. Today s Lecture

Computer Architecture

COMPUTER ORGANIZATION AND DESIGN

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

CS/COE1541: Introduction to Computer Architecture

All instructions have 3 operands Operand order is fixed (destination first)

Reduced Instruction Set Computer (RISC)

Computer Organization MIPS ISA

SPIM Instruction Set

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Reduced Instruction Set Computer (RISC)

MIPS Instruction Reference

MIPS Instruction Set

Lecture 6 Decision + Shift + I/O

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Thomas Polzer Institut für Technische Informatik

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

MIPS%Assembly% E155%

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Topic Notes: MIPS Instruction Set Architecture

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

CS222: MIPS Instruction Set

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CS3350B Computer Architecture MIPS Instruction Representation

ECE369. Chapter 2 ECE369

Computer Architecture

CSc 256 Midterm (green) Fall 2018

CENG3420 Lecture 03 Review

Lecture 6: Assembly Programs

CS3350B Computer Architecture MIPS Introduction

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Course Administration

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Lecture 7: Examples, MARS, Arithmetic

Compiling Techniques

Computer Architecture. The Language of the Machine

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Lecture 5: Procedure Calls

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

F. Appendix 6 MIPS Instruction Reference

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

MIPS Instruction Set Architecture (2)

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Chapter 2: Instructions:

Instruction Set Design and Architecture

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

CS3350B Computer Architecture

Instructions: Language of the Computer

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Programming the processor

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Instruction Set Architecture. "Speaking with the computer"

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Q1: /30 Q2: /25 Q3: /45. Total: /100

Computer Architecture. MIPS Instruction Set Architecture

MIPS Assembly Language Programming

MIPS Functions and the Runtime Stack

TSK3000A - Generic Instructions

Lecture 4: MIPS Instruction Set

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Transcription:

MIPS Assembly Language 1 We will study the MIPS assembly language as an exemplar of the concept. MIPS assembly instructions each consist of a single token specifying the command to be carried out, and zero or more operation arguments: <opcode> arg0, arg1,... argn The tokens are separated by commas and (optionally) whitespace. Indentation is insignificant to the assembler, but is certainly significant to the human reader. MIPS command tokens are short and mnemonic (in principle). For example: add lw sw jr The MIPS reference card bound in the front of P&H includes a complete listing of all the MIPS commands you will need to understand and use. MIPS Assembly Language MIPS command arguments include: - hardware registers - offset and base register - literal constants (immediate arguments) - labels 2 Registers are specified either as $k, where 0 k 31, or using the symbolic names shown earlier. Of course, MIPS assembly also allows comments. Simply, all characters from a # character to the end of the line are considered a comment. There are also some special directives, but those can wait...

MIPS Hello World # PROGRAM: Hello, World! 3.data # Data declaration section out_string:.asciiz "\nhello, World!\n".text # Assembly language instructions main: # Start of code section li $v0, 4 # system call code for printing string = 4 la $a0, out_string # load address of string to be printed into $a0 syscall # call operating system to perform operation in $v0 # syscall takes its arguments from $a0, $a1,... This illustrates the basic structure of an assembly language program. - data segment and text segment - use of label for data object (which is a zero-terminated ASCII string) - use of registers - invocation of a system call MIPS Assembly Arithmetic Instructions All arithmetic and logical instructions have 3 operands 4 Operand order is fixed (destination first): <opcode> <dest>, <src1>, <src2> Design Principle: simplicity favors regularity. Example: C code: a = b + c; MIPS code : add $s3, $s1, $s5 The natural number of operands for an operation like addition is three requiring every instruction to have exactly three operands, no more and no less, conforms to the philosophy of keeping the hardware simple

Assembly Arithmetic Instructions Design Principle: simplicity favors regularity. 5 Of course this complicates some things... C code: a = b + c + d; MIPS pseudo-code: add a, b, c add a, a, d Operands must be registers (or immediates), only 32 registers are provided Each register contains 32 bits Design Principle: smaller is faster. Why? add addi addiu addu div mult multu sub subu... Immediates In MIPS assembly, immediates are literal constants. 6 Many instructions allow immediates to be used as parameters. addi $t0, $t1, 42 # note the opcode li $t0, 42 # actually a pseudo-instruction Note that immediates cannot be used with all MIPS assembly instructions; refer to your MIPS reference card. Immediates may also be expressed in hexadecimal: 0xFFFFFFFF

MIPS Assembly Logical Instructions Logical instructions also have 3 operands: 7 <opcode> <dest>, <src1>, <src2> Examples: and $s0, $s1, $s2 # bitwise AND andi $s0, $s1, 42 or $s0, $s1, $s2 # bitwise OR ori $s0, $s1, 42 nor $s0, $s1, $s2 # bitwise NOR (i.e., NOT OR) sll $s0, $s1, 10 # logical shift left srl $s0, $s1, 10 # logical shift right QTP: MIPS assembly doesn t include the logical operation not. Why? How would you achieve the effect of a logical not operation in MIPS assembly? Assembly Load and Store Instructions Transfer data between memory and registers 8 Example: C code: A[12] = h + A[8]; MIPS code: lw $t0, 32($s3) # load word add $t0, $s2, $t0 sw $t0, 48($s3) # store word Can refer to registers by name (e.g., $s2, $t2) instead of number Load command specifies destination first: Store command specifies destination last: opcode <dest>, <address> opcode <dest>, <address> Remember arithmetic operands are registers or immediates, not memory! Can t write: add 48($s3), $s2, 32($s3)

Addressing Modes In register mode the address is simply the value in a register: 9 lw $t0, ($s3) In immediate mode the address is simply an immediate value in the instruction: lw $t0, 0 In base + register mode the address is the sum of an immediate and the value in a register: lw $t0, 100($s3) There are also various label modes: j absval j absval + 100 j absval + 100($s3) An Assembly Language Example 10 Can we figure out the code? void swap(int v[], int k) { } int temp; temp = v[k] v[k] = v[k+1]; v[k+1] = temp; Conventions for procedure calls: - arg0 is in register $4 (aka $a0) - arg1 is in register $5 (aka $a1) - - return address is in register $31 ($ra) swap: # need label to jump to in call sll $t0, $a1, 2 # calculate offset of v[k] add $t0, $a0, $t0 # add offset to array base address lw $t2, 0($t0) # load v[k] into register lw $t3, 4($t0) # load v[k+1] into register sw $t3, 0($t0) # store register v[k] to v[k+1] sw $t2, 4($t0) # store register v[k+1] to v[k] jr $ra # return to caller

So far we ve learned 11 MIPS - loading words but addressing bytes - arithmetic on registers only # Instruction # Meaning add $s1, $s2, $s3 # $s1 = $s2 + $s3 sub $s1, $s2, $s3 # $s1 = $s2 $s3 lw $s1, 100($s2) # $s1 = Memory[$s2+100] sw $s1, 100($s2) # Memory[$s2+100] = $s1 Conditional Branch Instructions 12 Decision making instructions - alter the control flow, - i.e., change the "next" instruction to be executed MIPS conditional branch instructions: bne beq $t0, $t1, <label> # branch on not-equal # PC = &<label> if $t0!= $t1 $t0, $t1, <label> # branch on equal Labels are strings of alphanumeric characters, underscores and periods, not beginning with a digit. They are declared by placing them at the beginning of a line, followed by a colon character. if ( i == j ) h = i + j; bne $s0, $s1, Miss add $s3, $s0, $s1 Miss:...

Unconditional Branch Instructions 13 MIPS unconditional branch instructions: j Label # PC = Label jr $ra # PC = $ra if ( i!= j ) h = i + j; else h = i j; beq $s4, $s5, Lab1 add $s3, $s4, $s5 j Lab2 Lab1: sub $s3, $s4, $s5 Lab2:... Can you build a simple for loop? Conditional Set Instructions 14 MIPS conditional set instructions: slt $t0, $s0, $s1 # $t0 = 1 if $s0 < $s1 # $t0 = 0 otherwise slti $t0, $s0, <imm> # $t0 = 1 if $s0 < imm # $t0 = 0 otherwise There are similar unsigned versions. if ( i < j ) goto A; else goto B; # $s3 == i, $s4 == j slt $t1, $s3, $s4 beq $zero, $t1, B A: # code... j C B: # code... C:

So far we ve learned: 15 # Instruction Meaning add $s1,$s2,$s3 # $s1 = $s2 + $s3 sub $s1,$s2,$s3 # $s1 = $s2 $s3 lw $s1,100($s2) # $s1 = Memory[$s2+100] sw $s1,100($s2) # Memory[$s2+100] = $s1 bne $s4,$s5,l0 # Next instr. is at L0 if $s4 $s5 beq $s4,$s5,l1 # Next instr. is at L1 if $s4 = $s5 j Label # Next instr. is at Label Custom Control Flow 16 We have: beq, bne, what about Branch-if-less-than? Recall the set-less-than instruction: slt $t0, $s1, $s2 if ($s1 < $s2) $t0 = 1 else $t0 = 0; We can use this instruction to build "blt $s1, $s2, Label" can now build general control structures Note that the assembler needs a register to do this, there are policy of use conventions for registers

Program Termination 17 Unlike the high-level languages you are accustomed to, MIPS assembly does not include an instruction, or block syntax, to terminate the program execution. MIPS programs can be terminated by making a system call: ## Exit li $v0, 10 # load code for exit system call in $v0 syscall # make the system call to exit Without such code, the system would attempt to continue execution into the memory words that followed the final instructions of the program. That rarely produces graceful results. Policy of Use Conventions 18 MIPS programmers are expected to conform to the following conventions when using the 29 available 32-bit registers: Name Register number Usage $zero 0 the constant value 0 $v0-$v1 2-3 values for results and expression evaluation $a0-$a3 4-7 arguments $t0-$t7 8-15 temporaries $s0-$s7 16-23 saved $t8-$t9 24-25 more temporaries $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address Register 1 ($at) is reserved for the assembler, 26-27 for operating system

Pseudo-Instructions 19 You may have noticed something is odd about a number of the MIPS instructions that have been covered so far. For example: li $t0, 0xFFFFFFFF Now, logically there's nothing wrong with wanting to place a 32-bit value into one of the registers. But there's certainly no way the instruction above could be translated into a 32-bit machine instruction, since the immediate value alone would require 32 bits. This is an example of a pseudo-instruction. A MIPS assembler or interpreter like SPIM may be designed to support such extensions that make it easier to write complex programs. In effect, the assembler supports an extended MIPS architecture that is more sophisticated than the actual MIPS architecture of the underlying hardware. Of course, the assembler must be able to translate every pseudo-instruction into a sequence of valid MIPS assembly instructions. Pseudo-Instruction Examples 20 move $t1, $t2 # $t1 <-- $t2 or $t1, $t2, $zero # recall: x OR 0 == x li $t1, <imm> # $t1 = 32-bit imm value # e.g., suppose <imm> is 0x23A0FB17 # # The assembler sometimes needs a register in which it can # store temporary values. The register $at is reserved for # such use. lui $at, 0x23A0 # put upper byte in upper byte of reg, # and 0s in the lower byte ori $t1, $at, 0xFB18 # put lower byte into reg

lui and ori Details 21 We'd like to be able to load a 32-bit constant into a register Must use two instructions, new "load upper immediate" instruction lui $t0, 43690 # 1010101010101010 1010101010101010 0000000000000000 low-order bits filled with zeros Then must get the lower order bits right, i.e., ori $t0, $t0, 39321 # 1001100110011001 1010101010101010 0000000000000000 ori 0000000000000000 1001100110011001 1010101010101010 1001100110011001 MIPS Directives 22 Directives are special reserved identifiers used to communicate instructions to the assembler. Directives begin with a period character. For example:.data.text.align.space.word.byte.asciiz.globl # mark beginning of a data segment # mark beginning of a text (code) segment # specify memory alignment of data # allocate uninitialized space in memory # store values in successive words # store values in successive bytes # store zero-terminated character sequence # specify global scope for a label Note: technically, directives are not part of the MIPS assembly language, but rather are instructions to the assembler to take certain actions.

MIPS System Calls 23 Service Call code Arguments Result print integer 1 $a0 = integer to print print float 2 $f12 = float to print print double 3 $f12 = double to print print string 4 $a0 = addr of null-term string read integer 5 $v0 contains integer read read float 6 $f0 contains float read read double 7 $f0 contains double read read string 8 $a0 = address of input buffer $a1 = maximum # of chars to be read sbrk 9 $a0 = # of bytes to allocate $v0 contains address of allocated memory exit 10 print character 11 $a0 = character to print read character 12 $a0 contains character read MIPS System Calls 24 Service Call code Arguments Result open file 13 $a0 = addr of null-term string containing file name $a1 = flags $a2 = mode $a0 contains file descriptor read from file 14 $a0 = file descriptor $a1 = addr of input buffer $a2 = max # of chars to read $a0 contains number of chars read write to file 15 $a0 = file descriptor $a1 = address of output buffer $a2 = # of chars to write $a0 contains number of chars written close file 16 $a0 = file descriptor exit2 17 $0 = termination result

Data Basic fact: declaration: 25 at the machine language level there are no explicit data types, only contents of memory locations. The concept of type is present only implicitly in how data is used. reserving space in memory, or deciding that a certain data item will reside in a certain register. Directives are used to reserve or initialize memory:.data # mark beginning of a data segment.asciiz "a string" # declare and initialize a string.byte 13, 14, -3 # store values in successive bytes.space 16 # alloc 16 bytes of space.word 13, 14, -3 # store values in successive words A complete listing of MIPS/SPIM directives can be found in P&H Appendix A. Arrays 26 First step is to reserve sufficient space for the array. Array elements are accessed via their addresses in memory, which is convenient if you ve given the.space directive a suitable label..data list:.word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 size:.word 10... la $t1, list # get array address li $t2, 0 # set loop counter print_loop: beq $t2, $t3, print_loop_end # check for array end lw $a0, ($t1) # print value at the array pointer li $v0, 1 syscall addi $t2, $t2, 1 # advance loop counter addi $t1, $t1, 4 # advance array pointer j print_loop # repeat the loop print_loop_end:

Array Example 27 This is part of the palindrome example from the course website:.data string_space:.space 1024... # prior to the loop, $t1 is set to the address of the first # char in string_space, and $t2 is set to the last one test_loop: bge $t1, $t2, is_palin # if lower pointer >= upper # pointer, yes lb $t3, ($t1) # grab the char at lower ptr lb $t4, ($t2) # grab the char at upper ptr bne $t3, $t4, not_palin # if different, it's not... addu $t1, $t1, 1 # advance lower ptr subu $t2, $t2, 1 # advance upper ptr j test_loop # repeat the loop MIPS Instruction Summary 28 MIPS assembly language Category Instruction Example Meaning Comments add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory load upper immediate lui $s1, 100 16 $s1 = 100 * 2 Loads constant in upper 16 bits branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to PC + 4 + 100 branch on not equal bne $s1, $s2, 25 if ($s1!= $s2) go to Conditional PC + 4 + 100 branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0 set less than immediate slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; else $s1 = 0 Equal test; PC-relative branch Not equal test; PC-relative Compare less than; for beq, bne Compare less than constant jump j 2500 go to 10000 Jump to target address Uncondi- jump register jr $ra go to $ra For switch, procedure return tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call

Other Issues 29 Not discussed yet: - support for procedures - linkers, loaders, memory layout - stacks, frames, recursion - manipulating strings and pointers - interrupts and exceptions - system calls and conventions Some of these we'll talk more about later