Intro Assembly Computer Organization I 1 June 2010 ©2006-10 McQuain, Feng & Ribbens MIPS Hello World # Program: Hello, World!.data # data declaration.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Lecture 5: MIPS Instruction Set
Branches Two branch instructions:
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
Chapter 2 Instructions: Language of the Computer
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Fall 2003SYCS-401 Operating Systems Instruction Set Architecture An overview of MIPS R3000 assembly language.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EE472 – Spring 2007P. Chiang, with Slide Help from C. Kozyrakis (Stanford) ECE472 Computer Architecture Lecture #3—Oct. 2, 2007 Patrick Chiang TA: Kang-Min.
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CDA 3101 Spring 2016 Introduction to Computer Organization
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
Chapter 2 Instructions: Language of the Computer.
MIPS Assembly.
CS2100 Computer Organisation
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MPIS Instructions Functionalities of instructions Instruction format
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
The University of Adelaide, School of Computer Science
Part II Instruction-Set Architecture
MIPS function continued
Computer Instructions
Flow of Control -- Conditional branch instructions
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
MIPS Assembly.
Flow of Control -- Conditional branch instructions
MIPS Coding Continued.
Computer Architecture
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
MIPS function continued
Conditional Control Structure
Presentation transcript:

Intro Assembly Computer Organization I 1 June 2010 © McQuain, Feng & Ribbens MIPS Hello World # Program: Hello, World!.data # data declaration section; specifies values to be stored # in memory and labels whereby the values are accessed Greeting:.asciiz "\nHello, World!\n".text # Start of code section main: # Execution begins at label "main" li $v0, 4 # system call code for printing string = 4 la $a0, Greetings # load address of string to be printed into $a0 syscall # call operating system to perform operation; # $v0 specifies the system function called; # syscall takes $v0 (and opt arguments) 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

Intro Assembly Computer Organization I 2 June 2010 © McQuain, Feng & Ribbens MIPS Register Names MIPS assemblers support standard symbolic names for the general-purpose registers: $zero stores value 0; should never be modified $v0-1 used for system calls and procedure return values $a0-3 used for passing arguments to procedures $t0-9 used for local storage; caller saves $s0-7 used for local storage; procedure saves $sp stack pointer; primarily used in procedure calls $fp frame pointer; primarily used during stack manipulations $ra used to store return address in procedure call $gp pointer to area storing global data (data segment) $at reserved for use by the assembler; DO NOT USE $k0-1 reserved for use by OS kernel; DO NOT USE

Intro Assembly Computer Organization I 3 June 2010 © McQuain, Feng & Ribbens MIPS Arithmetic Instructions All arithmetic and logical instructions have 3 operands Operand order is fixed (destination first):,, Example: C code: a = b + c; MIPS code: add $s0, $s3, $s2 “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”

Intro Assembly Computer Organization I 4 June 2010 © McQuain, Feng & Ribbens Basic MIPS Arithmetic Instructions add $rd,$rs,$rt Addition with overflow GPR[rd] <-- GPR[rs] + GPR[rt] div $rs,$rt Division with overflow $lo <-- GPR[rs]/GPR[rt]* $hi <-- GPR[rs]%GPR[rt] mul $rd,$rs,$rt Multiplication without overflow GPR[rd] <-- (GPR[rs]*GPR[rt])[31:0] sub $rd,$rs,$rt Subtraction with overflow GPR[rd] <-- GPR[rs] - GPR[rt] Here are the most basic arithmetic instructions: Instructions "with overflow" will generate an runtime exception if the computed result is too large to be stored correctly in 32 bits. There are also versions of some of these that essentially ignore overflow, like addu. * see mfhi and mflo

Intro Assembly Computer Organization I 5 June 2010 © McQuain, Feng & Ribbens Limitations and Trade-offs Design Principle: simplicity favors regularity. Design Principle: smaller is faster. Why? Operands must be registers (or immediates), only 29 user registers are provided Each register contains 32 bits Of course this complicates some things... C code: a = b + c + d; MIPS pseudo-code: add $s0, $s1, $s2 add $s0, $s0, $s3

Intro Assembly Computer Organization I 6 June 2010 © McQuain, Feng & Ribbens Immediates In MIPS assembly, immediates are literal constants. 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: 0x2A

Intro Assembly Computer Organization I 7 June 2010 © McQuain, Feng & Ribbens Example.text main: # set coefficients li $s0, 10 # c0 == 10 li $s1, 7 # c1 == 7 li $s2, 5 # c2 == 5 li $s3, 3 # set x == 3 # evaluate polynomial add $s4, $zero, $s2 # calculate c2 mul $s4, $s4, $s3 # c2 * x add $s4, $s4, $s1 # c2 * x + c1 mul $s4, $s4, $s3 # (c2 * x + c1) * x add $s4, $s4, $s0 # (c2 * x + c1) * x + c0 li $v0, 10 # exit program syscall

Intro Assembly Computer Organization I 8 June 2010 © McQuain, Feng & Ribbens MIPS Logical Instructions Logical instructions also have three operands and the same format as the arithmetic instructions:,, 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

Intro Assembly Computer Organization I 9 June 2010 © McQuain, Feng & Ribbens MIPS Load and Store Instructions Transfer data between memory and registers Example: C code: A[12] = h + A[8]; MIPS code: lw $t0, 32($s3) # $t0 <-- Mem[$s3+32] add $t0, $s2, $t0 sw $t0, 48($s3) # Mem[$s3+48] <-- $t0 Can refer to registers by name (e.g., $s2, $t2 ) instead of number Load command specifies destination first: opcode, Store command specifies destination last: opcode, Remember arithmetic operands are registers or immediates, not memory! Can’t write: add 48($s3), $s2, 32($s3)

Intro Assembly Computer Organization I 10 June 2010 © McQuain, Feng & Ribbens Example.data c2:.word 5 # coefficients for polynomial c1:.word 7 c0:.word 10 x:.word 3 # x value for evaluation pofx:.word # storage location for result.text main: # load coefficients and x from memory: lw $s0, c0 # c0 == 10 lw $s1, c1 # c1 == 7 lw $s2, c2 # c2 == 5 li $s3, x # x == 3 # computations from previous example go here sw $s4, pofx # save fn value to memory

Intro Assembly Computer Organization I 11 June 2010 © McQuain, Feng & Ribbens Additional Load and Store Instructions There are also load and store instructions that act on data objects smaller than a word: lhload half-word from specified address lbload byte from specified address shstore half-word to specified address sbstore byte to specified address There are restrictions: For word-oriented operations, the address must be word-aligned (i.e., a multiple of 4). For half-word-oriented operations, the address must be half-word-aligned (i.e., a multiple of 2). Violating the restrictions causes a runtime exception. (See unaligned variants.)

Intro Assembly Computer Organization I 12 June 2010 © McQuain, Feng & Ribbens Addressing Modes In register mode the address is simply the value in a register: lw $t0, ($s3) In immediate mode the address is simply an immediate value in the instruction: lw $t0, 0 # almost always a bad idea 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: lw $t0, absval lw $t0, absval lw $t0, absval + 100($s3)

Intro Assembly Computer Organization I 13 June 2010 © McQuain, Feng & Ribbens Computer Science Dept Va Tech January 2008 © McQuain & Ribbens MIPS conditional set instructions: slt $t0, $s0, $s1 # $t0 = 1 if $s0 < $s1 # $t0 = 0 otherwise slti $t0, $s0, # $t0 = 1 if $s0 < imm # $t0 = 0 otherwise These are useful for "remembering" the results of a Boolean comparison for later use. Conditional Set Instructions

Intro Assembly Computer Organization I 14 June 2010 © McQuain, Feng & Ribbens MIPS unconditional branch instructions: j Label # PC = Label b Label # PC = Label jr $ra # PC = $ra Unconditional Branch Instructions These are useful for building loops and conditional control structures.

Intro Assembly Computer Organization I 15 June 2010 © McQuain, Feng & Ribbens Decision making instructions -alter the control flow, -i.e., change the "next" instruction to be executed MIPS conditional branch instructions: bne $t0, $t1, # branch on not-equal # PC += 4 + Label if # $t0 != $t1 beq $t0, $t1, # 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. Conditional Branch Instructions

Intro Assembly Computer Organization I 16 June 2010 © McQuain, Feng & Ribbens Example # computes N + (N – 1 ) data N:.word 100 # specify limit for summation Sum:.word 0 # space to hold computed sum.text main: lw $s0, N # set loop counter to N li $s1, 0 # set sum to 0 loop: add $s1, $s1, $s0 # update running total addi $s0, $s0, -1 # decrement loop counter bne $s0, $zero, loop # repeat until counter reaches 0 sw $s1, Sum # save sum to memory li $v0, 10 # exit program syscall