Summer 2003 Lecture 12 06/26/03

Similar documents
Summer 2003 Lecture 11 06/25/03

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

PC Interrupt Structure and 8259 DMA Controllers

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Summer 2003 Lecture 15 07/03/03

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

ECE 485/585 Microprocessor System Design

SOEN228, Winter Revision 1.2 Date: October 25,

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

Summer 2003 Lecture 14 07/02/03

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.

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

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

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

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

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Programming Model 2 A. Introduction

PESIT Bangalore South Campus

Program Control Instructions

8086 Interrupts and Interrupt Responses:

Practical Malware Analysis

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

ORG ; TWO. Assembly Language Programming

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Intel Instruction Set (gas)

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

9/25/ Software & Hardware Architecture

Summary: Direct Code Generation

Computer Systems Lecture 9

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

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

Lecture (08) x86 programming 7

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Tutorial 1: Programming Model 1

Introduction to 8086 Assembly

Chapter 3. Assembly Language Programming with 8086

For more notes of DAE

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

UNIT 4. Modular Programming

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

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

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

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

IFE: Course in Low Level Programing. Lecture 6

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Registers. Registers

Midterm Exam #2 Answer Key

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

Overview of Input/Output Mechanisms

CS401 - Computer Architecture and Assembly Language Programming Glossary By

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

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Assembly Language Programming of 8085

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

Objectives. Saving Interrupt Vectors. Writing a Custom Interrupt Handler. Examples of use of System functions for Input-Output and Interrupts

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Segmentation in Assembly Language Programming

3.0 Instruction Set. 3.1 Overview

ELEC 242 Time Delay Procedure

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

Program Exploitation Intro

Changing the Embedded World TM. Module 3: Getting Started Debugging

Week 11 Programmable Interrupt Controller

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

Introduction to Assembly Language Programming (Instruction Set) 1/18/2011 1

LAB 3: Programming in Assembly Language

8085 INSTRUCTION SET INSTRUCTION DETAILS

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Topic 6: Code Generation

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

CS401 Assembly Language Solved MCQS From Midterm Papers

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

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Come and join us at WebLyceum

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

16.317: Microprocessor Systems Design I Fall 2014

Experiment 3 3 Basic Input Output

The CS5600 micro-computer

Microcontroller Intel [Instruction Set]

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

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

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Computer Architecture and Assembly Language Programming CS401 Lecture No: 1 Address, Data, and Control Buses A computer system comprises of a

CS 550 Operating Systems Spring Interrupt

System & Program Developments of 8051

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Computer System Architecture



EC 333 Microprocessor and Interfacing Techniques (3+1)

mith College Computer Science CSC231 Assembly Week #10 Fall 2017 Dominique Thiébaut

The due date for submitting this assignment has passed. 1) How many times will the following loop be executed? Depends on the initial value of A

SOURCE LANGUAGE DESCRIPTION

CHAPTER 11 INTERRUPTS PROGRAMMING

Architecture Project Phase (1)

EKT222 Miroprocessor Systems Lab 5

Transcription:

Summer 2003 Lecture 12 06/26/03 Implementing Standard C Control Structures: IF THEN ELSE if (a == b) { mov ax,a blah; cmp ax,b jnz endif blah endif: if (a == b) { true clause else { false clause mov ax,a mov ax,a cmp ax,b cmp ax,b jz doif jnz doelse doelse: doif: false clause true clause jmp endif jmp endif doif: doelse: true clause false clause endif: endif: The most complete form of the IF control construct adds additional ELSE IF tests to add more possible choices. if (a == b) { mov ax,a first clause cmp ax,b jnz test2

else if (a == c) { second clause first clause else if (a == d) { jmp endif third clause test2: cmp ax,c jnz test3 else { final clause second clause jmp endif test3: cmp ax,d jnz doelse third clause jmp endif doelse: final clause endif: WHILE while (a == b) { ltop: mov ax,a cmp ax,b jnz lend jmp ltop lend: DO WHILE do { ltop: while (a == b); mov ax,a cmp ax,b jz ltop

FOR LOOP for (<init expr>; <test expr>; <reinit expr>) { This is semantically equivalent to the following C constructs: init expr; while (test expr) { reinit expr;

The implementation of this in assembler follows directly from the above description for the implementation of the WHILE loop. for (inx = 0; inx < 10; inx++) {movinx,0 ltop: mov ax,inx cmp ax,10 jae lend add inx,1 jmp ltop lend: SWITCH-CASE The SWITCH construct in C allows for a multi-way decision. An expression is evaluated, and resulting value is used to select a particular block of code to execute from several choices. The C syntax looks like: switch (expr) { case const-expr: statements case const-expr: statements default: statements The value of expr is compared with the value of the const-expr associated with each case statement. If a match occurs, control is transferred to the statements associated with that case. If no match is found, control transfers to the optional default case. If no default case is present, control transfers to the end of the case list. Normally, control will flow from the statements associated with one case into the next unless a break statement is used to cause control to transfer to the end.

This can be implemented in one of two ways in assembler. In the case where the values for the case statement const-expr s are not a contiguous range of values, it is most efficiently implemented using an IF- THEN-ELSE like construct. The equivalent C for this would be: val = expr; if (val == const_expr1) { case 1 statements else if (val == const_expr2) { case 2 statements else { default statements The implementation of this in assembler is exactly as shown above for the IF- THEN-ELSE construct. Note: The C semantics for the switch statement is that control flows through from one case to the next. To prevent this, it is necessary to put break statements after each case. If this is done, the semantics of the switch statement is exactly the same as an IF-THEN-ELSE construct. If the break statements are not included and control flows through from one case to the next, it is necessary to put in explicit jumps to get the proper control flow, and then the semantics are similar to, but not exactly the same as an IF-THEN-ELSE.

Alternatively, if the values of the switch expression are contiguous (e.g. 0, 1, 2, 3) the most efficient implementation is to use a jump table. In this case, the value of the switch expression is used as the index into a table of pointers to the blocks of statements associated with each case. An indirect jump using the indexed location in the jump table is used to transfer control to the appropriate statement block. Note: When using a jump table, it is important to check that the switch expression value is within the range of values allowed for the jump table. If an out of range value is used to index into the jump table, the program will crash.

The jump table can be placed either in the code segment or the data segment. A code segment implementation will be illustrated: switch (inx) { mov si,inx case 0: cmp si,0 case 0 body jb ldefault break; cmp si,3 case 1: ja ldefault case 1 body add si,si break; jmp jmptab[si] case 2: jmptab dw lcase0 case 2 body dw lcase1 break dw lcase2 default: lcase0: default body case 0 body jmp lend lcase1: case 1 body jmp lend lcase2: case 2 body jmp lend ldefault default body lend: Note: In the case where the value of the first case expression isn t 0, it is necessary to subtract that value from the switch value before computing the index into the jump table.

One of the advantages of programming in assembly language is that there is a great deal more flexibility available in implementing the flow of control in the program. It is not necessary to implement the assembler code exactly as shown above for the C language constructs. However, it is also much easier in assembler to create buggy, impossible to understand code by undisciplined coding. It is generally advisable to use a disciplined, structured approach to implementing the flow of control in an assembler program and to use structures similar to those shown above. Interrupt Service Routines The interrupt service routine is the subroutine that handles the interrupt. Its address is stored in the interrupt vector table. When an interrupt occurs, the CPU could be doing anything, and could have values in any register or flag that are important. The Interrupt Service routine, ISR, has no way of knowing what the interrupted code was doing when the interrupt occurred, or which registers are important, so it must preserve all CPU state. The first thing that most ISR s do is to push all registers that they are going to modify, to preserve their values. Note that the FLAGS register was automatically pushed onto the stack before control was transferred to the ISR, so it isn t necessary to push the flags. The ISR then performs whatever tasks are necessary to service the device that signaled the interrupt. After servicing the device, the ISR will restore the registers by popping their values from the stack and then perform an IRET instruction to return to the interrupted task.

Assume, for example, that the interrupt was generated by a characteroriented device, such as a serial port. A device like a serial port will have both an input stream and an output stream of characters. As each character is received coming into the port, an interrupt will be generated. The interrupt service routine will read the character from the input register of the serial port, and typically will store the character into an input queue or circular buffer. The higher-level program that is consuming characters from the serial port will read them from the input buffer when they become available. By using an interrupt driven input queue, the consuming application is freed from the necessity of always having to be able to service the serial port before a character is lost. Similarly, on the output side, the application will typically write the output characters into an output queue. The serial port hardware will generate an interrupt whenever the output buffer register on the serial port is empty and ready to send another character. The interrupt service routine will read the next character from the output queue and write it to the output buffer of the serial port hardware.

Debugger Trace and Breakpoint Interrupts There are two features of the interrupt system on the 8088/8086 that are intended specifically to support the writing of debuggers. The first is the single step interrupt or trace interrupt (INT 1). The second is the breakpoint interrupt (INT 3). The single step or trace interrupt mechanism allows the debugger to easily execute a single instruction in the debug child program and then return control to the debugger. When the trace flag (TF ) is set in the FLAGS register, the CPU will execute one instruction and then perform an INT 1 operation, as if an INT 1 instruction had been executed. The debugger will hook into the INT 1 vector and point it at its single step entry point. To execute the child program, the debugger will set up all of the child program s registers and place an IRET frame on the child s stack, with the FLAGS entry on the stack having the trace flag set. When the debugger performs an IRET, control transfers to the child program and executes one instruction. After executing that instruction, the CPU performs the INT 1, returning control to the debugger, which then saves the child registers into its local state variables, switches to its own stack and register contents and then returns to its own internal operation. The breakpoint interrupt works in a similar manner, in that the interrupt mechanism is used to return control to the debugger after executing some number of child program instructions. The INT 3 instruction is a special one byte version of the general INT instruction. The breakpoint opcode is 0CCh. The debugger can set a breakpoint by saving the first opcode byte of the instruction where the breakpoint is to occur and then replacing that byte with the 0CCh opcode. The debugger returns control to the child program again by building an IRET frame on the stack, restoring the child registers and then performing an IRET to resume execution of the child s code. When the breakpoint instruction is executed, the CPU will perform an INT 3 operation. The debugger will have hooked the INT 3 vector with the address of its breakpoint entry point. The breakpoint entry point will save all of the child s registers, switch to the debugger stack, restore the original opcode byte that the breakpoint had replaced, and then resume the internal operation of the debugger.

It is important that the breakpoint instruction be a single byte instruction. If the breakpoint required multiple bytes, it could overlap two or more instructions, and make it impossible to reliably set a breakpoint on the desired instruction in some cases.