Chapter 3 (Part a) Assembly Language Fundamentals

Similar documents
Assembly Language Programming

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

Assembly Language for Intel-Based Computers, 5 th Edition

Computer Organization and Assembly Language. Lab Session 3

Lab 2: Introduction to Assembly Language Programming

Assembly Language for Intel-Based Computers, 5 th Edition. Kip Irvine. Chapter 3: Assembly Language Fundamentals

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals.

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 3: Assembly Language Fundamentals

EECE416 :Microcomputer Fundamentals and Design. X86 Assembly Programming Part 1. Dr. Charles Kim

Assembly Fundamentals

Chapter 3: Assembly Language Fundamentals. Cristina G. Rivera

Assembly Language Fundamentals

Introduction to Assembly Language

Assembly Language Fundamentals

Introduction to Assembly Language

Assembly Language LAB

Assembly Language. Lecture 3 Assembly Fundamentals

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

Assembly Language for Intel-Based Computers, 4 th Edition

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Assembly Language Fundamentals. Chapter 3

Chapter 6 (Part a) Conditional Processing

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

Assembly Language: g Part III. First Semester 2013 Department of Computer Science Faculty of Science Chiang Mai University

Chapter 3: Addressing Modes

Practical Malware Analysis

Module 3 Instruction Set Architecture (ISA)

Basic Assembly SYSC-3006

ORG ; TWO. Assembly Language Programming

We will first study the basic instructions for doing multiplications and divisions

EEM336 Microprocessors I. Addressing Modes

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

Ethical Hacking. Assembly Language Tutorial

SOEN228, Winter Revision 1.2 Date: October 25,

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

MICROPROCESSOR TECHNOLOGY

Assembly Language. Lecture 5 Procedures

Assembler Programming. Lecture 10

Assembly Language. Lecture 5 Procedures

CPU. IBM PC and compatible Memory Structure

Integer Arithmetic Part2

Course Syllabus [1/2]

Machine and Assembly Language Principles

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

UNIT 4. Modular Programming

Machine Code and Assemblers November 6

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

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

ALT-Assembly Language Tutorial

8086 ALP TOOLS (CH 2) CHAPTER 2

Assembly basics CS 2XA3. Term I, 2017/18

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

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

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

By: Dalbir Singh, Computer Science Dep't

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

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

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

Faculty of Engineering Student Number:

Variable and Data Type I

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

CS 3843 Final Exam Fall 2012

Assembly Language Lab # 9

DEPARTMENT OF MATHS, MJ COLLEGE

CSE 505 Lecture 8: Introduction to Assembly Language

EC-333 Microprocessor and Interfacing Techniques

Chapter 2. Lexical Elements & Operators

Writing 32-Bit Applications

UNIT- 3 Introduction to C++

Full file at C How to Program, 6/e Multiple Choice Test Bank

JAVA Programming Fundamentals

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Chapters 3. ARM Assembly. Embedded Systems with ARM Cortext-M. Updated: Wednesday, February 7, 2018

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Computer Science Final Examination Wednesday December 13 th 2006

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

COMPUTER ENGINEERING DEPARTMENT

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Mating Assembly with C

Assembly Language. Instructor: Dmitri A. Gusev. Spring Lecture 10, February 27, CSC : Introduction to Computer Science

X86-NASM STANDARD COMMANDS. Comment your code with a semicolon (;)! The assembler won t read anything after it.

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

SPIM & MIPS Programming

INTRODUCTION 1 AND REVIEW

Lecture 2 Tao Wang 1

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Programming for Engineers Introduction to C

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

Computer Organization and Assembly Language. Lab Session 01

ELEC 242 Time Delay Procedure

A Fast Review of C Essentials Part I

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Department Chapter 7. Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie. Chapter 7

BASIC ELEMENTS OF A COMPUTER PROGRAM

Transcription:

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2025: Assembly Language Discussion Chapter 3 (Part a) Assembly Language Fundamentals Eng. Eman R. Habib February, 2014

2 Assembly Language Discussion Integer Constants: [{+ }] digits [radix] Note:- Elements within square brackets [..] are optional. Elements within braces {..} require a choice of one of the enclosed elements (separated by the character). Elements in italics denote items that have known definitions or descriptions. Radix may be one of the following (uppercase or lowercase): h Hexadecimal b Binary q/o Octal d Decimal r Encoded real y Binary (alternate) t Decimal (alternate) If no radix is given, the integer constant is assumed to be decimal. 26 Decimal 42o Octal 26d Decimal 1Ah Hexadecimal 11010011b Binary 0A3h Hexadecimal 42q Octal Integer Expressions: -(3-4) * (6-1) = -35-3 - 4 * 6-1 = 20 25 mod 3 = 1

3 Assembly Language Discussion Real Number Constants [{+,-}] integer.[integer][ E[{+,-}]integer] 2. +3.0-44.2E+05 26.E5 Character Constants Single character enclosed in single or double quotes. 'A' "d" String Constants Sequence of characters (including spaces) enclosed in single or double quotes: 'ABC' 'X' "Good night " '4096' Reserved Words Instruction mnemonics, such as MOV, ADD, and MUL Register names Directives, which tell MASM how to assemble programs Attributes, which provide size and usage information for variables and operands. Examples are BYTE and WORD Operators, used in constant expressions Predefined symbols, such as @data, which return constant integer values at assembly time Identifiers It might identify a variable, a constant, a procedure, or a code label. They may contain between 1 and 247 characters. They are not case sensitive. The first character must be a letter (A..Z, a..z), underscore (_), @,?, or $. Subsequent characters may also be digits. An identifier cannot be the same as an assembler reserved word.

4 Assembly Language Discussion Directives A directive is a command embedded in the source code that is recognized and acted upon by the assembler. Directives do not execute at runtime. Directives can define variables, macros, and procedures. They can assign names to memory. In MASM, directives are case insensitive. Defining Segments:.DATA directive identifies the area of a program containing variables..code directive identifies the area of a program containing executable instructions..stack directive identifies the area of a program holding the runtime stack, setting its size:.stack 100h. Instructions An instruction is a statement that becomes executable when a program is assembled. [label:] mnemonic [operands] [;comment] Translated by the assembler into machine language. Executed by the CPU at runtime. An instruction contains four basic parts: Label (optional) Instruction mnemonic (required) Operand(s) (usually required) Comment (optional) Code Labels Must end with a colon (:) character. Used as targets of jumping and looping instructions. Example: target: mov ax,bx... jmp target Instruction Mnemonic Short word that identifies an instruction. mov Move (assign) one value to another

5 Assembly Language Discussion add sub mul jmp call Add two values Subtract one value from another Multiply two values Jump to a new location Call a procedure Operands Instructions can have between zero and three operands. Can be a register, memory operand, constant expression, or input-output port. No operands: stc ; set Carry flag One operand: inc eax ; add 1 to EAX Two operands: mov count,ebx ; move EBX to count The first operand is called the destination. The second operand is the source. Three operands: imul eax,ebx,5 ; eax = ebx * 5 The first operand is the destination, and the following 2 operands are source operands. Comments Single-line comments, beginning with a semicolon character (;), end by new line Block comments, beginning with the COMMENT directive and a user-specified symbol, end by the same user-specified symbol appears. Example: COMMENT! This line is a comment. This line is also a comment.! We can also use any other symbol: COMMENT & This line is a comment. This line is also a comment. &

6 Assembly Language Discussion Section 3.1 Review 1. Identify valid suffix characters used in integer constants. h,q,o,d,b,r,t,y 3. (Yes/No): Does the multiplication operator (*) have a higher precedence than the division operator (/) in integer expressions? No (they have the same precedence) 4. Write a constant expression that divides 10 by 3 and returns the integer remainder. 10 MOD 3 5. Show an example of a valid real number constant with an exponent. 26.E5 7. Reserved words can be instruction mnemonics, attributes, operators, predefined symbols, and directives. 8. What is the maximum length of an identifier?247 characters 13. Name the four basic parts of an assembly language instruction. label, mnemonic, operand(s), comment 14. (True/False): MOV is an example of an instruction mnemonic. 16. Show an example of a block comment. Comment! This is a comment This is also a comment! 17. Why would it not be a good idea to use numeric addresses when writing instructions that access variables? Because the addresses coded in the instructions would have to be updated whenever new variables were inserted before existing ones.

7 Assembly Language Discussion Example: Adding and Subtracting Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers..386.model FLAT, STDCALL INCLUDE Irvine32.inc.code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h exit main ENDP END main call DumpRegs ; display registers TITLE Add and Subtract (AddSub.asm) This line is optional. The TITLE directive marks the entire line as a comment. ; This program adds and subtracts 32-bit integers. Comment..386 The.386 directive identifies the minimum CPU required for this program..model flat,stdcall In the current.model directive, the flat keyword tells the assembler to generate code for a protected mode program, and the stdcall keyword enables the calling of MS-Windows functions. INCLUDE Irvine32.inc The INCLUDE directive copies necessary definitions and setup information from a text file named Irvine32.inc..code The.code directive marks the beginning of the code segment, where all executable statements in a program are located. main PROC The PROC directive identifies the beginning of a procedure. The name chosen for the only procedure in our program is main.

8 Assembly Language Discussion mov eax,10000h ; EAX = 10000h The MOV instruction moves (copies) the integer 10000h to the EAX register. The first operand (EAX) is called the destination operand, and the second operand is called the source operand. The comment on the right side shows the expected new value in the EAX register. add eax,40000h ; EAX = 50000h The ADD instruction adds 40000h to the EAX register. sub eax,20000h ; EAX = 30000h The SUB instruction subtracts 20000h from the EAX register. call DumpRegs ; display registers The CALL statement calls a procedure that displays the current values of the CPU registers. This can be a useful way to verify that a program is working correctly. exit The exit statement (indirectly) calls a predefined MS-Windows function that halts the program. Note that exit is not a MASM keyword; instead, it s a macro command defined in the Irvine32.inc include file that provides a simple way to end a program. Expanded into a call to ExitProcess that terminates the program, ExitProcess function is defined in the kernel32 library. We can replace exit with the following: push 0 ; push parameter 0 on stack call ExitProcess ; to terminate program You can also replace exit with: INVOKE ExitProcess, 0 There is a need to declare that ExitProcess is an external function defined outside the addsub.asm program. We use the PROTO directive for this purpose as shown below: ExitProcess PROTO, ExitCode:DWORD This PROTO directive also specifies the parameters and types of a given function. main ENDP The ENDP directive marks the end of the main procedure. END main The END directive marks the last line of the program to be assembled. It identifies the name of the program s startup procedure (the procedure that starts the program execution). Section 3.2 Review 1. In the AddSub program (Section 3.2), what is the meaning of the INCLUDE directive? The INCLUDE directive copies necessary definitions and setup information from the Irvine32.inc text file. The data from this file is inserted into the data stream read by the assembler.

9 Assembly Language Discussion 2. In the AddSub program, what does the.code directive identify?.code directive identifies the area of a program containing executable instructions. 3. What are the names of the segments in the AddSub program? code, data, and stack. 4. In the AddSub program, how are the CPU registers displayed? call DumpRegs 5. In the AddSub program, which statement halts the program? exit 6. Which directive begins a procedure? PROC 7. Which directive ends a procedure? ENDP 8. What is the purpose of the identifier in the END statement? It identifies the name of the program s startup procedure 9. What does the PROTO directive do? ROTO declares the name of a procedure that is called by the current program. Assembling, Linking, and Running Programs Step 1: A programmer uses a text editor to create an ASCII text file named the source file. Step 2: The assembler reads the source file and produces an object file, a machine-language translation of the program. Optionally, it produces a listing file. If any errors occur, the programmer must return to Step 1 and fix the program. Step 3: The linker reads the object file and checks to see if the program contains any calls to procedures in a link library. The linker copies any required procedures from the link library, combines them with the object file, and produces the executable file. Optionally, it produces (.map) file Step 4: The operating system loader utility reads the executable file into memory and branches the CPU to the program s starting address, and the program begins to execute.

10 Assembly Language Discussion Section 3.3 Review 1. What types of files are produced by the assembler? Object (.OBJ) and listing (.LST) files. 2. (True/False): The linker extracts assembled procedures from the link library and inserts them in the executable program. 3. (True/False): When a program s source code is modified, it must be assembled and linked again before it can be executed with the changes. 4. Which operating system component reads and executes programs? Loader 5. What types of files is produced by the linker? Executable (.EXE) and map (.MAP). Best Wishes