Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.

Slides:



Advertisements
Similar presentations
Introduction to Assembly Language
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Programming Notes for Practical2 Munaf Sheikh
Assembly Language for x86 Processors 6th Edition
Assembly Language for Intel-Based Computers, 5th Edition
CS2422 Assembly Language and System Programming Assembly Language Fundamentals Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming September 19, 2006.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Symbolic Constants Equal-Sign Directive Calculating.
Runtime Stack Managed by the CPU, using two registers
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Intrinsic Data Types (1 of 2) BYTE, SBYTE 8-bit unsigned.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language Advantages 1. It reveals the secret of your computer’s hardware and software. 2. Speed. 3. Some special applications and occasions. Disadvantages.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
1 Lecture 3: Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Assembly Language Basic Concepts IA-32 Processor Architecture.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Assembly programming A little background on using the software.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Introduction to Assembly Language
CS2422 Assembly Language & System Programming October 24, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Coding.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
CS2422 Assembly Language & System Programming September 26, 2006.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Chapter 2 Software Tools and Assembly Language Syntax.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Chapter 3: Assembly Language Fundamentals
Assembly Fundamentals Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/13 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
Assembly Language for x86 Processors 7th Edition
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved.
Chapter 1: Basic Concepts
ZONG Wen Department of Computer Science and Engineering The Chinese University of Hong Kong
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 ICS 51 Introductory Computer Organization Fall 2009.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Assembly Language for x86 Processors 6th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You may modify.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
CS2422 Assembly Language and System Programming 0 Week 7 & 8 Intro. To Assembly Programming.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
1 Chapter 3 Assembly Language Fundamentals Assembly Language for Intel-Based Computers, 4th edition Revised 3/11/05.
Assembly Language for x86 Processors 6th Edition
Assembly language programming
Assembly language.
Format of Assembly language
Assembly Lab 3.
Assembly Language programming
x86 Assembly Language Fundamentals
Assembly Language Fundamentals
Microprocessor and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Introduction to Assembly Language
Assembly Language for Intel-Based Computers
Computer Architecture CST 250
Hardware & Software Architecture
Computer Architecture and System Programming Laboratory
BASIC SYNTAX OF ASSEMBLY LANGUAGE “HELLO WORLD” PROGRAM.
Presentation transcript:

Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition  Constants Memory Register

Example: Adding 3 Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc.code main PROC mov eax,10000h; EAX = 10000h add eax,40000h; EAX = 50000h sub eax,20000h; EAX = 30000h call DumpRegs; display registers exit main ENDP END main

Program Description Directives vs. Instructions.  Use Directives to tell assembler what to do.  Use Instructions to tell CPU what to do. Procedure defined by:  [Name] PROC  [Name] ENDP Instruction Format:  LABEL (optional), Mnemonic, Operands

Directives Commands that are recognized and acted upon by the assembler  Not part of the Intel instruction set  Used to declare code, data areas, select memory model, declare procedures, etc.

Instructions Assembled into machine code by assembler Executed at runtime by the CPU Member of the Intel IA-32 instruction set Parts  Label  Mnemonic  Operand

I/O Not as easy as you think, if you program it yourself. We will use the library provided by the author of the textbook. Two steps:  Include the library (Irvine32.inc) in your code.  Call the subroutines.

Assemble and Run! The required software comes with the book:  MASM: Microsoft Macro Assembler  Visual Studio: A Debugger  Irvine32.inc: I/O procedures

Assemble-Link Execute Cycle The following diagram describes the steps from creating a source program through executing the compiled program. If the source code is modified, Steps 2 through 4 must be repeated.

Steps to Run AddSub.asm C:\MASM>ML -Zi -c -Fl -coff addsub.asm //generate AddSub.obj and AddSub.lst C:\MASM>LINK32 addsub.obj irvine32.lib kernel32.lib /SUBSYSTEM:CONSOLE /DEBUG /MAP //generate Addsub.exe, Addsub.ilk, Addsub.pdb, and Addsub.map C:\MASM>addsub //show the content of registers

Using make32.bat … ML -Zi -c -Fl -coff %1.asm … LINK32 %1.obj irvine32.lib kernel32.lib /SUBSYSTEM:CONSOLE /DEBUG /MAP … dir %1.* … C:\MASM615>make32 c:\MASM\AddSub

Using Visual Studio to Assemble, Run, and Debug AddSub.asm /vs6/index.htm /vs6/index.htm Demo

Another Example TITLE Add and Subtract (AddSub2.asm) INCLUDE Irvine32.inc.data Val1 DWORD 10000h Val2 DWORD 40000h Val3 DWORD 20000h finalVal DWORD ?.code main PROC mov eax,Val1; EAX = 10000h add eax,Val2; EAX = 50000h sub eax,Val3; EAX = 30000h mov finalVal1,eax; store the result call DumpRegs; display registers exit main ENDP END main

Data Declaration [Label], Type, Initialization (or just ?)  Example: Var1 BYTE 7 Use ? if no initialization necessary.  Example: Var1 BYTE ? Other data types: (See Table 3-2 in p.81)  WORD (or DW),  DWORD (or DD), …etc.

Signed vs. Unsigned Signed vs. Unsigned:  SBYTE vs. BYTE  SWORD vs. WORD  … etc. Example:  Var1 BYTE 255  Var1 SBYTE -1

Characters and Strings How about characters? A few examples:  Var1 BYTE ‘A’  Var1 BYTE 41h  S1 BYTE ‘Hello, World!’  Line BYTE ‘ ’

How About A List or Array? Just list them! For example:  List1 BYTE 10, 32, 41h You may also mix them:  List1 BYTE 10, 32, ?, ‘A’  List2 BYTE “good”, ‘o’, ‘r’, ‘b’, ‘a’, ‘d’, 0

DUP Good for allocating space for a string or array. Examples:  Var1 BYTE 20 DUP (0)  Var2 BYTE 20 DUP (?)  Var3 BYTE4 DUP (“STACK”)

Another Example INCLUDE Irvine32.inc C1 EQU 40000h.data Val1 DWORD 10000h.code main PROC mov eax,Val1; EAX = 10000h add eax,C1; EAX = 50000h call DumpRegs; display registers exit main ENDP END main

EQU is for Constants EQU for constants. Also OK to use = for integers. Examples:  COUNT = 500  COUNT EQU 500 Good programming style: COUNT = 500. mov al,COUNT

Expression and Text in EQU OK to use expressions in EQU:  Matrix1 EQU 10 * 10  Matrix1 EQU No expression evaluation if within. EQU accepts texts too:  Msg EQU

Memory Organized like mailboxes, numbered 0, 1, 2, 3, …, 2 n -1.  Each box can hold 8 bits (1 byte)  So it is called byte-addressing. …

Byte? Word? The number has limited range. 1 Byte = 8 Bits:  Binary: to  Hexadecimal: 00 to FF  Decimal: 0 to 255 Word = 2 or 4 bytes, depending on the machine. In 80x86, it means 2 bytes.

Number Systems Binary & hexadecimal numbers. Conversion between them and decimal. How to represent negative numbers (in 2 ’ s compliment).

Memory Address Byte Addressing: each memory location has 8 bits. If we have only 16 bytes …  4 bits are enough for an address What if we have 64K? 1M? 1G? …

Memory Address 16-bit address is enough for up to 64K 20-bit for 1M 32-bit for 4G

Character String So how are strings like “ Hello, World! ” are stored in memory? ASCII Code! (or Unicode … etc.) Each character is stored as a byte. Review: how is “ 1234 ” stored in memory?

Integer A byte can hold an integer number:  between 0 and 255 (unsigned) or  between – 128 and 127 (2 ’ s compliment) How to store a bigger number? Review: how is 1234 stored in memory?

Big or Little Endian? Example: 1234 is stored in 2 bytes. = in binary = 04 D2 in hexadecimal Do you store 04 or D2 first? Big Endian: 04 first. Little Endian: D2 first.  Intel ’ s choice

Little Endian Order All data types larger than a byte store their individual bytes in reverse order. The least significant byte occurs at the first (lowest) memory address. Example:  val1 DWORD h Demo

Reason for Little-Endian? More consistent for variable length (e.g., 2 bytes, 4 bytes, 8 bytes … etc.)

Registers

General-Purpose:  AX, BX, CX, DX: 16 bits  Splitted into H and L parts, 8 bits each.  Extended into E?X to become 32-bit register (i.e., EAX, EBX, … etc.).

Convention EAX: accumulator EBX: base register ECX: count register EDX: data register

Other Registers We will explain their meaning when we encounter them later this semester: Segment (CS, DS, SS, ES) Pointer (EIP, ESP, EBP) Index (ESI, EDI) EFLAGS

Homework1 /Project1.doc /Project1.doc