Transfer of Control. Lecture 10 JMP. JMP Formats. Jump Loop Homework 3 Outputting prompts Reading single characters

Similar documents
Experiment 3 3 Basic Input Output

ORG ; TWO. Assembly Language Programming

Lecture 13: I/O I/O. Interrupts. How?

Lecture 8: Control Structures. Comparing Values. Flags Set by CMP. Example. What can we compare? CMP Examples

Intel 8086: Instruction Set

9/25/ Software & Hardware Architecture

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Assembling, Linking and Executing 1) Assembling: .obj obj .obj.lst .crf Assembler Types: a) One pass assembler:

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

PESIT Bangalore South Campus

EC-333 Microprocessor and Interfacing Techniques

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

PHY4635/5635 Spring Lecture 8: Program Control Instructions

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Chapter 3: Addressing Modes

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

EEM336 Microprocessors I. Data Movement Instructions

.code. lea dx,msg2. Page 1/8. Problem 1: Programming in Assembly [25 Points]

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Assembly Language Each statement in an assembly language program consists of four parts or fields.

EC 333 Microprocessor and Interfacing Techniques (3+1)

Assembly Language. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. Overview of Assembly Language

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

EEM336 Microprocessors I. Addressing Modes

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Experiment 8 8 Subroutine Handling Instructions and Macros

CS401 Assembly Language Solved MCQS From Midterm Papers

Module 3 Instruction Set Architecture (ISA)

ECpE 185 Laboratory Hand Assembly Fall 2006

Homework 2. Lecture 6: Machine Code. Instruction Formats for HW2. Two parts: How to do Homework 2!!!!

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

EC 333 Microprocessor and Interfacing Techniques (3+1)

UNIT 4. Modular Programming

Lecture 5 Program Logic and Control

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

CC411: Introduction To Microprocessors

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Computer Architecture 1 ح 303

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Arithmetic and Logic Instructions And Programs

Basic Assembly SYSC-3006

EXPERIMENT NINE DEVELOPING MACRO SEQUENCES

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Philadelphia University Student Name: Student Number:

Lecture 5:8086 Outline: 1. introduction 2. execution unit 3. bus interface unit

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Code segment Stack segment

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

1. Introduction to Assembly Language

Addressing Modes on the x86

ADVANCE MICROPROCESSOR & INTERFACING

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

MICROPROCESSOR TECHNOLOGY

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

8086 INTERNAL ARCHITECTURE

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

if 2 16bit operands multiplied the result will be

Arithmetic Instructions

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Assembly Language Lab # 9

Microprocessors (A) DOS Services

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Chapter 5 The LC-3. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2

INT 21H and INT 10H Programming and Macros

Experiment #5. Using BIOS Services and DOS functions Part 1: Text-based Graphics

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

The x86 Architecture

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

EEL 5722C Field-Programmable Gate Array Design

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Intel 8086 MICROPROCESSOR. By Y V S Murthy

Lab 6: Conditional Processing

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Assembly Language Lab # 2 Assembly Language Fundamentals

Come and join us at WebLyceum

Basic characteristics & features of 8086 Microprocessor Dr. M. Hebaishy

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

Constants and Expressions. Lecture 7: Assembly Language Programs. Constants and Expressions (cont.) Statements. Names. Assembly Directives

Computer Architecture and System Software Lecture 06: Assembly Language Programming

COSC121: Computer Systems: Review

EC-333 Microprocessor and Interfacing Techniques

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

Assembly Language: IA-32 Instructions

3- ADDRESSING MODES in 8086: In this section we use the MOV instruction to describe the data-addressing modes. Figure 3-1 shows the MOV instruction.

Lab 4: Basic Instructions and Addressing Modes

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

Basic Assembly Instructions

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Introduction to Computer Engineering. Chapter 5 The LC-3. Instruction Set Architecture

Transcription:

Lecture 10 Jump Loop Homework 3 Outputting prompts Reading single characters Transfer of Control The CPU loads and executes programs sequentially. You d like to be able to implement if statements, gotos, and loops. These involve transfer of control, where the order of statement execution is modified. Two types: Unconditional Transfer the program branches to a new location in all cases execution continues at the new address Conditional Transfer the program branches is a certain condition is true (like the conditional jumps) JMP JMP provides unconditional transfer. JMP address ; unconditional xfer ;to address ;(IP gets a new ; value) address is a user-defined label In machine code, to JMP to an address which is calculated by adding the displacement in the instruction to the value in IP or, for longer jumps, replacing the value in IP (and CS if it s a very long jump). In assembly, the assembler replaces the user labels with the offset during assembly (much easier!). JMP Formats Three basic forms of (direct) JMP instructions: JMP SHORT destination JMP NEAR PTR destination JMP FAR PTR destination There are different machine code formats for each of these. 1

JMP SHORT If the target label (address) is within 128 to 127 locations (bytes) of the instruction following the JMP (remember, the offset is added to the current value of the IP, which is pointing to the next instruction), it is assembled as a SHORT instruction (2 bytes). Only eight bits are needed to specify the address (these eight bits are added to the IP) Example Offset Machine Code Source Code 0100 B4 02 start: mov ah, 2 ;loop start 0102 B2 41 mov dl, A ; 0104 CD 21 int 21h ;disp A 0106 EB F8 jmp start ;jmp back 0108.. (rest of program) How does it know it s a SHORT jump? You re jumping back so the assembler already knows the distance! Symbol Table Symbol Value start 0100 When assembling, the assembler uses a location counter to keep track of where it is. When jmp start is assembled, the location counter = 0108 To compute displacement: 0100 0108 = -8 = F8 JMP NEAR PTR JMP FAR PTR What if the jump in the example had been forward? If the JMP was used without the SHORT qualifier it would have assumed to be JMP NEAR PTR. NEAR PTR indicates that the label can be anywhere in the current code segment. A 16-bit displacement is moved to the IP. FAR PTR allows you to jump outside of your current code segment. The label s segment address is moved to CS, and its offset is moved to IP. This is a 5-byte instruction: 1 for opcode 2 for displacement (-> IP) 2 for segment (->CS) 2

LOOP LOOP is the easiest way to do iteration. It s like a for-loop it s for countcontrolled loops only where the number of repetitions are known before the loop is entered. What is it good for: execute a loop 12 times (for example) repeat instructions x times What it isn t good for: keep looping until the user types enter LOOP, continued Format: LOOP destination What loop does: CX is the loop counter LOOP subtracts one from CX If CX is NOT equal to zero, control transfers to destination Example: mov cx, 5 ;cx = 5 mov ax, 0 ;ax = 0 start: add ax, 1 loop start ;jump to start after loop: ax = 5, cx = 0 Another LOOP example In C: ax = 0; for (i = 23; i >= 1; i--) { ax = ax + bx; } In Assembly: sub ax, ax ;or mov ax, 0 mov cx, 23 start: add ax, bx ;ax = ax + bx loop start ;if cx >= 1, ;jumps to start Loop: Errors to Avoid! Starting with CX = 0 LOOP will decrement CX to FFFFh and the loop will repeat 65,535 times!!! Altering the loop counter If CX (or CH or CL) is modified inside the loop then the loop will not behave the way you want it to! For example, if you increment CX then the loop will never stop. Also: Flags are not affected when LOOP decrements CX even when CX goes to zero! 3

Conditional Loops LOOPZ/LOOPE The assembler also has conditional loops. These loops still decrement CX and terminate when it is zero but they check other flags as well. LOOPZ/LOOPE (loop if zero, loop if equal) LOOPNZ/LOOPNE (loop if not zero, loop if not equal) LOOP while ZF = 1 and CX > 0 Example, p. 203 in Irvine LOOPNZ/LOOPNE Loop while ZF = 0 and CX > 0 Example from p. 204, Irvine Conditional Loop Pitfalls When you used an unconditional loop, you needed to be careful that you did not modify CX. You still need to be careful with CX but now you ALSO need to make sure you don t modify the flags unintentionally. Example p. 203, Irvine 4

In-Class Exercise Outputting Prompts Write assembly code that calculates the Fibonacci series: 1,1,2,3,5,8,13, (Except for the first two numbers in the sequence, each number is the sum of the preceding two numbers). Use LOOP and set the limit for 12 iterations. Friday s lecture will talk about MS-DOS function calls. You ll need to use one in your homework to output prompts to the user. INT 21h Outputting the Prompt INT 21h is a DOS function call (DOS services) You saw one example of this in hw2: mov ax, 4c00h int 21h ;don t forget h! This returns to DOS from an executing program. The 4Ch that goes into AH tells DOS which function to perform (in this case, returning to DOS). For hw3:.data prompt1 db "Enter the month: $".code.startup ;prompt for first input mov ah, 09h mov dx, offset prompt1 int 21h What is this doing? 09h: String output function writes a string to standard output AH = 09, DX = offset of the string. The string must be terminated by a $ (dollar sign character) 5

More on Output If you want to output a string, followed by a carriage return and line feed (crlf), you will need to put them in your string also! example, p. 148 of Irvine Warning! The $ at the end of the string tells DOS when the string has ended. If you try to print out a string that includes a $, it will not work (and you need to use a different method to output the $) If you forget the $ DOS will output all subsequent characters in memory until it finds the ASCII value for $ (24h). This could involve many screens worth of garbage output before it stops! Reading in Characters 01h filtered input with echo waits for a single character to be entered (or, if one is in the input buffer already just grabs it) stores it in AL Input: ah = 1 Output: al = the character read (filtered? filters out control characters) (echo? it displays the character you as you type it. If you weren t using echo you would not be able to see what you typed!) Example For hw 3, put 01h into ah, then use int 21h to read a character typed by the user: mov ah, 01h ;get first ;month digit int 21h The character read in will be in AL. You ll need to convert it from an ASCII character to a decimal number: sub al, 30h ;convert to ;decimal 6

Disadvantages of using 01h: only one character at a time it will read any character typed, as it is typed. if you type the wrong character you can t backspace and correct it it has already been read and processed by your program 7