Conditional Control Structure

Slides:



Advertisements
Similar presentations
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
Advertisements

CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
Arrays First step is to reserve sufficient space for the array.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Assembly Language (cont)
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
SPIM and MIPS programming
Chapter 2 Instructions: Language of the Computer
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Lecture 8: MIPS Instruction Set
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
MIPS I/O and Interrupt. .data val1:.float 0.6 val2:.float 0.8 msg_done:.asciiz "done\n".text.globl main main: li.s $f0, mfc1 $a0, $f0 jal calsqrt.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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,
Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put 'typical constants' in memory.
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.
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Instructions Language of The Computer (MIPS) 2.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i == j ) h = i + j;
Addressing Modes. Register Addressing Immediate Addressing Base Addressing Indexed Addressing PC-Relative Addressing.
Indexed Addressing addition to implementing new instructions, the extended assembler implements a new addressing mode. This is indexed addressing, a mode.
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Control Structures Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Conditional Control Structure if ( i < j ) goto A; else.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
Intro Assembly Computer Organization I 1 June 2010 © McQuain, Feng & Ribbens MIPS Hello World # Program: Hello, World!.data # data declaration.
CSCI206 - Computer Organization & Programming
MIPS Instruction Set Advantages
Arrays First step is to reserve sufficient space for the array.
Loading a Single Byte There are two instructions that load a byte from a memory address. The instructions differ in how the 8-bit byte is put into the.
MIPS Coding Continued.
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Conditional Control Structure
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
MIPS coding.
Pick up the handout on your way in!!
Lecture 4: MIPS Instruction Set
CSCI206 - Computer Organization & Programming
MPIS Instructions Functionalities of instructions Instruction format
Henk Corporaal TUEindhoven 2010
MIPS Instruction Encoding
ECE232: Hardware Organization and Design
MIPS coding.
MIPS Instruction Encoding
Lecture 7: Examples, MARS, Arithmetic
Instruction encoding The ISA defines Format = Encoding
MIPS coding.
MIPS Coding.
The University of Adelaide, School of Computer Science
MIPS function continued
MIPS Coding.
Instruction encoding The ISA defines Format = Encoding
COMS 361 Computer Organization
COMS 361 Computer Organization
Instruction encoding The ISA defines Format = Encoding
Instruction encoding The ISA defines Format = Encoding
MIPS Coding Continued.
MIPS coding.
Addressing Modes Don Porter.
Program Assembly.
9/27: Lecture Topics Memory Data transfer instructions
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

Conditional Control Structure bne $s0, $s1, skip add $s3, $s0, $s1 skip: .... if ( i == j ) h = i + j; beq $s0, $s1, if b skip if: add $s3, $s0, $s1 skip: .... Which is more efficient? Why? CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 1

Conditional Control Structure if ( i < j ) ++i; else ++j; # $s3 == i, $s4 == j blt $s3, $s4, if addi $s4, $s4, 1 b skip if: addi $s3, $s3, 1 skip: # $s3 == i, $s4 == j bge $s3, $s4, else addi $s4, $s4, 1 b skip else: addi $s4, $s4, 1 skip: CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 2

for Loop Example int Sum = 0; for (int i = 1; i <= N; ++i) { Sum = Sum + i; } # $s0 == Sum, $s1 == N, $t0 == i move $s0, $zero # register assignment lw $s1, N # assume global symbol li $t0, 1 # literal assignment loop: bgt $t0, $s1, done # loop test add $s0, $s0, $t0 # Sum = Sum + i addi $t0, $t0, 1 # ++i b loop # restart loop done: CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 3

for Loop Improved int Sum = 0; for (int i = 1; i <= N; ++i) { Sum = Sum + i; } # $s0 == Sum, $s1 == N, $t0 == i move $s0, $zero # register assignment lw $s1, N # assume global symbol ble $s1, $zero, done # check for trivial case li $t0, 1 # literal assignment loop: add $s0, $s0, $t0 # Sum = Sum + i addi $t0, $t0, 1 # ++i bgt $s1, $t0, loop # loop test done: CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 4

Program Termination 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 could attempt to continue execution into the memory words that followed the final instructions of the program. That rarely produces graceful results. CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 5

Pseudo-Instructions Board work: Binary Numbers 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 MARS, 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. Board work: Binary Numbers CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 6

Pseudo-Instruction Examples 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 0x23A0FB18 # # 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 Board work: Binary Numbers CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 7

lui and ori Details 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, 0x23A0 # 0010001110100000 0010001110100000 0000000000000000 low-order bits filled with zeros Then must get the lower order bits right, i.e., ori $t0, $t0, FB18 # 1111101100011000 0010001110100000 0000000000000000 0000000000000000 1111101100011000 ori 0010001110100000 1111101100011000 CS@VT October 2009 ©2006-09 McQuain, Feng & Ribbens 8