Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition."— Presentation transcript:

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

2 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

3 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

4 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.

5 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

6 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.

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

8 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.

9 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

10 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

11 Using Visual Studio to Assemble, Run, and Debug AddSub.asm http://www.nuvisionmiami.com/books/asm/ide /vs6/index.htm http://www.nuvisionmiami.com/books/asm/ide /vs6/index.htm Demo

12 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

13 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.

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

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

16 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

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

18 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

19 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

20 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

21 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. …

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

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

24 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? …

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

26 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?

27 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?

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

29 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 12345678h Demo

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

31 Registers

32 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.).

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

34 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

35 Homework1 http://dclab.cs.nthu.edu.tw/~course/Assembly /Project1.doc http://dclab.cs.nthu.edu.tw/~course/Assembly /Project1.doc


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

Similar presentations


Ads by Google