Dr. D.M. Akbar Hussain

Similar documents
Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

16.317: Microprocessor Systems Design I Fall 2013

16.317: Microprocessor Systems Design I Fall 2014

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

Assembler Programming. Lecture 10

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

16.317: Microprocessor Systems Design I Fall 2015

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

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

UNIVERSITY OF CALIFORNIA, RIVERSIDE

PHY4635/5635 Spring Lecture 8: Program Control Instructions

Segmentation in Assembly Language Programming

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

Summer 2003 Lecture 15 07/03/03

Summary: Direct Code Generation

Summer 2003 Lecture 14 07/02/03

1: /********************************************************** 2: * A

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

16.317: Microprocessor Systems Design I Spring 2015

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

3: 4: 5: 9: ',0 10: ',0 11: ',0 12: 13:.CODE 14: INCLUDE

We will begin our study of computer architecture From this perspective. Machine Language Control Unit

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

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

EE 332 Real Time Systems Midterm Examination Solution Friday February 13, :30 pm to 4:30 pm

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

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

PESIT Bangalore South Campus

Intel 8086: Instruction Set

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

A4 Sample Solution Ch3

Module 3 Instruction Set Architecture (ISA)

Selection and Iteration. Chapter 7 S. Dandamudi

EC 333 Microprocessor and Interfacing Techniques (3+1)

CALL instruction and RET instruction :

ORG ; TWO. Assembly Language Programming

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

UNIT 4. Modular Programming

COE 205 Lab Manual Experiment N o 12. Experiment N o Using the Mouse

Lecture (08) x86 programming 7

Chapter 3: Addressing Modes

Copyright 2000 by Barry B. Brey The CPU Scheduling Processes

Computer Architecture and System Software Lecture 07: Assembly Language Programming

CS401 - Computer Architecture and Assembly Language Programming Glossary By

Chapter Three Addressing Mode MOV AX, BX

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

Summer 2003 Lecture 4 06/14/03

CS401 Assembly Language Solved MCQS From Midterm Papers

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.

if 2 16bit operands multiplied the result will be

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

EEM336 Microprocessors I. Addressing Modes

CSC 8400: Computer Systems. Using the Stack for Function Calls

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Computer Architecture and System Software Lecture 06: Assembly Language Programming

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Program Exploitation Intro

Intermediate Code Generation

Assembly Language Programming of 8085

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

Procedure-Calling Conventions October 30

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

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

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

Assembly Language Programming of 8085

CSE351 Spring 2018, Midterm Exam April 27, 2018

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

CSC 2400: Computer Systems. Using the Stack for Function Calls

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

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

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Unoptimized Code Generation

CS241 Computer Organization Spring 2015 IA

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Procedures and the Stack. Chapter 4 S. Dandamudi

Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]

Lesson 4. Stack, Procedures and Macros

Program Control Instructions

Computer Systems Lecture 9

LABORATORY WORK NO. 8 WORKING WITH MACROS AND LIBRARIES

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

SOURCE LANGUAGE DESCRIPTION

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Real instruction set architectures. Part 2: a representative sample

CS 2210 Programming Project (Part IV)

EEM336 Microprocessors I. Data Movement Instructions

UW CSE 351, Winter 2013 Midterm Exam

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

Computer Architecture 1 ح 303

Transcription:

1 2 Compiler Construction F6S 1

3 4 Compiler Construction F6S 2

5 6 Compiler Construction F6S 3

7 8 Compiler Construction F6S 4

a=b*- c + b*- c 9 a=b*- c + b*- c 10 Compiler Construction F6S 5

a=b*- c + b*- c 11 12 Compiler Construction F6S 6

Grammar Rules exp1 id = exp2 exp aexp aexp1 aexp2 + factor aexp factor factor (exp) factor num factor id Semantic Rules exp1.pcode = lda id.strval ++exp2.pcode ++ stn exp.pcode = aexp.pcode exp1.pcode p = aexp2.pcode p ++factor.pcode ++ adi aexp.pcode = factor.pcode factor.pcode = exp.pcode factor.pcode = ldc num.strval factor.pcode = lod id.strval 13 Synthesized 3AC Grammar Rules exp1 id = exp2 exp aexp aexp1 aexp2 + factor Semantic Rules exp1.name = exp2.name exp1.tacode = exp2.tacode ++ id.strval = exp2.name exp.name = aexp.name exp.tacode = aexp.tacode exp1.name = newtemp () aexp1.tacode = aexp2.tacode ++ factor.tacode ++ aexp1.name = aexp2.name + factor.name aexp factor aexp.name = factor.name aexp.tacode = factor.tacode factor (exp) factor.name = exp.name factor.tacode = exp.tacode factor num factor.name = num.strval factor.tacode = factor id factor.name = id.strval factor.tacode = 14 Compiler Construction F6S 7

Practical Code Generation 15 Practical Code Generation 16 Compiler Construction F6S 8

Generating Target Code from IC 17 3 x Address of x 3 + x Address of x x + 3 4 x + 3 x + 3 + 4 18 Compiler Construction F6S 9

3 x Address of x T1 Address of x T1 4 T1 T2 19 T2 + x, T1 + 4 x 3 20 Compiler Construction F6S 10

x 100 21 a *(a + i) i a a + s * i 22 Compiler Construction F6S 11

23 24 Compiler Construction F6S 12

25 26 Compiler Construction F6S 13

27 28 Compiler Construction F6S 14

29 30 Compiler Construction F6S 15

31 32 Compiler Construction F6S 16

33 34 Compiler Construction F6S 17

35 36 Compiler Construction F6S 18

37 38 Compiler Construction F6S 19

39 40 Compiler Construction F6S 20

41 42 Compiler Construction F6S 21

(x = x+3 ) + 4 Variable x in this expression is stored locally on the stack frame. Assembly code for this expression: mov ax, word ptr [bp-2] add ax, 3 mov word ptr [bp-2], ax add ax, 4 Register ax is used as the main temporary location for the computation. Location of the local variable x is bp-2. bp base pointer register as the frame pointer and integer variables occupy two bytes on this machine. The first instruction moves the value of the x to ax an indirect rather than an immediate load). (the brackets in the addresses [bp-2] indicate The second instruction adds the constant 3 to this register. The third instruction then moves this value to the location of x. Finally, the forth instruction adds 4 to ax, so that the final vale of the expression is left in this register, where it may be used for further computations. Note the address of x for this assignment in the third instruction is not pre-computed (as an lda P-code instruction would suggest). A static simulation of the intermediate code, together with knowledge of available addressing modes, can delay the computation of the address of x until this point. 43 44 Compiler Construction F6S 22

if and while 45 46 Compiler Construction F6S 23

Consider Definition now for f: _f proc near push bp mov bp,sp add ax,word ptr [bp+4] inc ax jmp short @1@58 @1@58: pop bp ret _f endp The return address is on the stack between the control link (the old bp) and the argument as a result of the caller s execution of a call instruction. This, the old bp is at the top of the stack, the return addressisatlocationbp+2 (addresses are two bytes in this example), the parameter x is at location bp+4, and the parameter y is at location bp+6. Thebodyoff then corresponds to the code that comes next: Mov ax,word ptr [bp+4] Add ax,word ptr [bp+6] Inc ax which loads x into ax, addsy to it, and then increment it by one. 47 48 Compiler Construction F6S 24

49 The code generated for this statement: x.j = x.i; mov ax, word ptr [bp-6] (loads x.i into ax) mov word ptr [bp-3], ax (stores this value to x.j) The offset computation for j( 6 +3= 3) is performed statically by the compiler. The code generated for the statement: p->lchild=p; mov word ptr [si+2], si Note how the indirection and the offset computation ti are combined into a single instruction. Finally, the code generated for the statement: p=p->rchild; mov si, word ptr [si+4] 50 Compiler Construction F6S 25

51 cmp bx,dx jle short @1@86 inc dx jmp short @1@114 @1@86: dec bx @1@114: This code uses the same sequential organization shown earlier but note this code does not compute the actual logical value of the expression x > ybut simply uses the condition code directly. The code generated by the Borland compiler for the while-statement is as follows: jmp short @1@170 @1@142: sub dx,bx @1@170: cmp dx,bx jl short @1@142 This uses a slightly different sequential organization given earlier, here test is placed at the end, and an initial unconditional jump is made to this test. 52 Compiler Construction F6S 26

53 54 Compiler Construction F6S 27

55 56 Compiler Construction F6S 28

57 58 Compiler Construction F6S 29

59 60 Compiler Construction F6S 30

61 62 Compiler Construction F6S 31

x=1;.. y=0; if (y) x = 10;. if (x) y = 100; Constant Propagation x=1; y=0;. if (0) x = 10;. if (x) y = 100; Un-reachable x = 1; y = 0;. if (x) y = 100; 63 64 Compiler Construction F6S 32

65 66 Compiler Construction F6S 33

Example Flow Graph 67 68 Compiler Construction F6S 34