Natawut NupairojAssembly Language1 Control Structure.

Slides:



Advertisements
Similar presentations
Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.
Advertisements

CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Machine Instructions Control Flow 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1B.ppt Modification date: March 24, 2015.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Comp Sci Control structures 1 Ch. 5 Control Structures.
Lesson 5 - Decision Structure By: Dan Lunney
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
LAB Flow Control Instructions
Assembly Language for Intel-Based Computers
Flow Control Instructions
Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers.
CPSC355 Week 3 Hoang Dang. Week 3 Assignment 1 Do/while loop Assignment 2 Binary Bitwise operation.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides4-1.ppt Modification date: Oct 22, 2014.
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC Architecture Dr. Anu Bourgeois 1.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Computer Science Selection Structures.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC Architecture Dr. Anu Bourgeois 1.
Making Decision – Microprocessor
Natawut NupairojAssembly Language1 Memory and Stack.
Natawut NupairojAssembly Language1 Arithmetic Operations.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
ECE Lecture 1 1 L7 – A First Program Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
(Flow Control Instructions)
1 ICS 51 Introductory Computer Organization Fall 2009.
ECE Lecture 1 1 L11-HLL to Assembler Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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)
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Natawut NupairojAssembly Language1 Pipelining Processor.
CPS120: Introduction to Computer Science Decision Making in Programs.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
תרגול 5 תכנות באסמבלי, המשך
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Ch2a- 2 EE/CS/CPE Computer Organization  Seattle Pacific University Taking orders A computer does what you tell it to do Not necessarily what.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Chapter 2 SPARC Architecture Chinua Umoja 1.  SPARC is a load/store architecture  Registers used for all arithmetic and logical operations  32 registers.
SPARC Programming Model 24 “window” registers 8 global registers Control registers –Multiply step –PSR (status flags, etc.) –Trap Base register –Window.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
ARM Instructions ARM instructions are written as an operation code (opcode), followed by zero or more operands Operands may be constants (8-bit value),
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Unit 1 Instruction set M.Brindha AP/EIE
Chapter 15: Higher Level Constructs
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
1. Introduction A microprocessor executes instructions given by the user Instructions should be in a language known to the microprocessor Microprocessor.
Computer Architecture & Operations I
Decision Making.
More Branch Instructions and Set Instructions
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
Morgan Kaufmann Publishers Computer Organization and Assembly Language
More on logical instruction and
The University of Adelaide, School of Computer Science
Machine-Level Programming: Control Flow
CSC 3210 Computer Organization and Programming
Program Logic and Control
Program Logic and Control
Flow Control Instructions
Chapter 7 –Program Logic and Control
Chapter 7 –Program Logic and Control
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Natawut NupairojAssembly Language1 Control Structure

Natawut NupairojAssembly Language2 Control Structures Write an algorithm first and then convert it to an assembly program. Learn how to translate basic control structures into machine language. Basic control structures: –do loop –while loop –for loop –if-then-else

Natawut NupairojAssembly Language3 Comparison Instruction To compare two values (register vs. register or register vs. constant): cmp%o2, 30 or cmp%g2, %i1 The result of this instruction effects the following flags: Z - whether the result was zero. N - whether the result was negative. V - whether the result was overflow. C - whether the result generated a carry.

Natawut NupairojAssembly Language4 Branch Instructions The comparison instruction can be used with branch instructions: –babranch always = goto –bnbranch never = nop –blbranch on less (than zero) –blebranch on less or equal (to zero) –bebranch on equal (to zero) –bnebranch on not equal (to zero) –bgebranch on greater or equal (to zero) –bgbranch on greater (than zero)

Natawut NupairojAssembly Language5 Do…While Loop Structure: do { some statements here; } while(logical expression); Translate to: loop:assembly code for S1 assembly code for S2 … Sn assembly code for L1 branch to loop if L1 is true S 1 …S n L1L1

Natawut NupairojAssembly Language6 Our Second Program Let modify our first program to include a loop: main() { int x, y; x = 0; do { y = ((x - 1) * (x - 7)) / (x - 11); x = x + 1; } while (x < 11); printf(“%d\n”, y); }

Natawut NupairojAssembly Language7 Our Second Program /* Variables * Store x in %l0 * Store y in %l1 */ fmt:.asciz “%d\n”.align 4.global main main:save %sp, -64, %sp clr %l0! x = 0 loop:sub %l0, 1, %o0!(x - 1) to %o0 sub %l0, 7, %o1!(x - 7) to %o1 call.mul!(x - 1)*(x - 7) nop

Natawut NupairojAssembly Language8 Our Second Program sub %l0, 11, %o1!(x - 11) to %o1 call.div!(x-1)*(x-7)/(x-11) nop mov %o0, %l1! Store it in y add %l0, 1, %l0! x = x + 1 cmp %l0, 11! x < 11 ? bl loop nop! Delay slot

Natawut NupairojAssembly Language9 Our Second Program set fmt, %o0 mov %l1, %o1 call printf! printf(“%d\n”, y); nop mov 1, %g1! Exit request ta 0

Natawut NupairojAssembly Language10 While Loop The condition is tested first. while (logical expression) { some statements here; }; Translate to: loop:assembly code for L1 branch to exit if L1 is false assembly code for S1 assembly code for S2 … Sn unconditional branch to loop done: S 1 …S n L1L1

Natawut NupairojAssembly Language11 While Loop Example while(a <= 17) { a = a + b; c = c + 1; } loop:cmp %l0, 17 bg done! Branch if L1 is false nop! Delay slot add %l0, %l1, %l0! a = a + b add %l2, 1, %l2! c = c + 1 ba loop! Always branch to “loop” nop! Delay slot done: L1L1 S 1 and S 2

Natawut NupairojAssembly Language12 For Loop Structure: for( ex1 ; ex2 ; ex3 ) st; Translate to: ex1; while( ex2 ) { st; ex3; }

Natawut NupairojAssembly Language13 For Loop Example for (a=1 ; a <= b ; a++) { c = c * a; } Translate to: a = 1; while( a <= b ) { c = c * a; a++;/* a = a + 1 */ } Note: a = %l0, b = %l1, c = %l2

Natawut NupairojAssembly Language14 For Loop Example mov 1, %l0! a = 1; loop:cmp %l0, %l1! Compare a and b. bg exit! Exit for-loop if a > b. nop mov %l2, %o0! First param for.mul mov %l1, %o1! Second param for.mul call.mul! %o0 = c * b nop mov %o0, %l2! Store result in c add %l0, 1, %l0! a++; ba test nop exit:

Natawut NupairojAssembly Language15 If-Then Structure: if( ex1 ) { st; } Test "ex1" Skip "st" if ex1 is false

Natawut NupairojAssembly Language16 If-Then Example d = a; if((a+b) > c) { a = a + b; c = c + 1; } a = c + d; NOTE: –a = %l0 –b = %l1 –c = %l2 –d = %l3 –tmp = %l4-- keep a+b value

Natawut NupairojAssembly Language17 If-Then Example mov %l0, %l3! d = a; add %l0, %l1, %l4! tmp = (a + b) cmp %l4, %l2 ble next! Jump if((a+b) <= c) nop! Delay slot ! Inside if statement. add %l0, %l1, %l0! a = a + b; add %l2, 1, %l2! c = c + 1; ! End of inside if statement. next: add %l2, %l3, %l0! a = c + d;

Natawut NupairojAssembly Language18 If-Then-Else Structure: if( ex1 ) { st1; } else { st2; } Test "ex1" If ex1 is false, goto ”ELSE- BLOCK:”. IF-BLOCK: –Execute “st1” –Goto “DONE:” ELSE-BLOCK: –Execute “st2” DONE:

Natawut NupairojAssembly Language19 If-Then-Else Example d = a; if((a+b) > c) { a = a + b; c = c + 1; } else { d = c; } a = c + d;

Natawut NupairojAssembly Language20 If-Then-Else Example mov %l0, %l3! d = a; add %l0, %l1, %l4! tmp = (a + b) cmp %l4, %l2 ble else-block! Jump if((a+b) <= c) nop! Delay slot ! if-block: Inside if statement. add %l0, %l1, %l0! a = a + b; add %l2, 1, %l2! c = c + 1; ba done! Skip the else-block. nop! Delay slot ! End of inside if statement.

Natawut NupairojAssembly Language21 If-Then-Else Example else-block: ! Inside else-block. mov %l2, %l3! d = c ! End of else-block. done: add %l2, %l3, %l0! a = c + d;