CPSC 240 L6V5-Assembly Language Fundamentals(Chpt 3) PDF

Title CPSC 240 L6V5-Assembly Language Fundamentals(Chpt 3)
Course Computer Organization and Assembly Language
Institution California State University Fullerton
Pages 20
File Size 733.6 KB
File Type PDF
Total Downloads 12
Total Views 141

Summary

Professor's name was Eric Shulman....


Description

CPSC 240_L6V5-AssemblyLanguageFundamental(Chpt 3)s.docx 9-6-2016 (Corrected Section 5.6) This lecture is from Chapter 3 in Kip Irvine’s Assembly Language of X86 Processors 6th Ed Table of Contents 1

2 3

4

5

6

Basic Elements of Assembly Language ..................................................................................................................... 2 1.1 Integer Constants ...................................................................................................................................................... 2 1.2 Integer Expressions ................................................................................................................................................. 2 1.3 Character and String Constants........................................................................................................................... 3 1.4 Reserved Words and Identifiers ......................................................................................................................... 3 1.5 Directives ..................................................................................................................................................................... 4 1.6 Instructions ................................................................................................................................................................. 4 Adding and Subtracting Integers ................................................................................................................................ 6 Coding Standard and Example Programs ................................................................................................................ 6 3.1 Add and subtract 32-bit Integers Example Using the Irvine Libs .......................................................... 6 3.2 Add and subtract 32-bit Integers Example Using No Irvine Libs ........................................................... 7 3.3 The Basic Program Template for your programs will be .......................................................................... 7 Assembling, Linking, and Running Programs ........................................................................................................ 8 4.1 A Listing File ............................................................................................................................................................... 8 4.2 Map files ..................................................................................................................................................................... 10 Defining Data .................................................................................................................................................................... 10 5.1 Intrinsic Data Types ............................................................................................................................................... 11 5.2 Data Definition Statement ................................................................................................................................... 11 5.3 Defining BYTE and SBYTE Data ........................................................................................................................ 11 5.4 Defining Byte Arrays .............................................................................................................................................12 5.5 Defining Strings ....................................................................................................................................................... 12 5.6 Using the DUP Operator ....................................................................................................................................... 13 5.7 Defining WORD and SWORD Data (16-bits) ................................................................................................13 5.8 Defining DWORD and SDWORD Data (32-bit) ............................................................................................ 14 5.9 Defining QWORD, TBYTE, Real Data ............................................................................................................... 14 5.10 Little Endian Order .............................................................................................................................................. 15 5.11 Adding Variables to AddSub ............................................................................................................................ 15 5.12 Declaring Uninitialized Data ............................................................................................................................ 16 5.13 Symbolic Constants ............................................................................................................................................. 16 5.13.1 Equal-Sign Directive ................................................................................................................................... 16 5.13.2 Calculating the Size of a Byte Array ...................................................................................................... 17 5.13.3 The EQU Directive........................................................................................................................................17 5.13.4 TEXTEQU Directive ..................................................................................................................................... 18 64-Bit Programming (Irvine 7th Ed, Section 3.6) ................................................................................................ 19

This lecture is covering just the Assembly Language Fundamentals. We will be going into more depth as the course proceeds. 1

1 Basic Elements of Assembly Language Integer constants Integer expressions Character and string constants Reserved words and identifiers Directives and instructions Labels Mnemonics and Operands Comments Examples 1.1 • • •

Integer Constants Optional leading + or – sign binary, decimal, hexadecimal, or octal digits Common radix characters: • h – hexadecimal • d – decimal • b – binary • r – encoded real Examples: 30d, 6Ah, 42, 1101b Hexadecimal beginning with letter: 0A5h (If starts with a letter, start with zero)

1.2

Integer Expressions

Operators and precedence levels

Examples Expression 16 / 5 -(3 + 4) * (6 – 1) -3 + 4 * 6 - 1 25 mod 3

Value 3 -35 20 1

Comment It is an integer

Modulus operator 2

1.3

Character and String Constants

A character constant is a single character enclosed in a single or double quotes. MASM stores the value in memory as the character’s binary ASCII code. Examples are ‘A’ or “d” An ASCII char is 1 byte

Embedded quotes are permitted when used in the manner shown below ‘This isn’t a test” ‘Say “Good night,” Gracie’ 1.4

Reserved Words and Identifiers

Reserved words can not be used as identifiers and include Instruction mnemonics such as MOV, ADD, MUL, etc Register names Directives which tell MASM how to assemble programs Attributes which provide size and usage information for variables and operands such as BYTE and WORD Operators used in constant expressions Predefined symbols such as @data which will return constant integer values at assembly time Some MASM examples are $ CARRY? OVERFLOW? ? DWORD PARITY? @B FAR PASCAL @F FAR16 QWORD ADDR FORTRAN REAL4 BASIC FWORD REAL8 BYTE NEAR REAL10 C NEAR16 SBYTE

SDWORD SIGN? STDCALL SWORD SYSCALL TBYTE VARARG WORD

Register Names you need to avoid AH CS ECX TR3 DR0 GS AL CX EDI BP DS TR4 AX DR1 EDX BX DX TR5 BH DR2 ES CH EAX TR6 BL DR3 SI CL EBP TR7 CR0 DR6 SP DH ESI CR2 DR7 SS DI ESP CR3 EBX ST DL FS CR – Control Register TR – Task Register 3

ZERO?

DR – Debug Register

1.5 Directives Assembly directives, also called pseudo-opcodes, pseudo-operations or pseudo-ops, are instructions that are executed by an assembler at assembly time, not by a CPU at run time. The names of pseudo-ops often start with a dot to distinguish them from machine instructions. Pseudo-ops can make the assembly of the program dependent on parameters input by a programmer, so that one program can be assembled different ways, perhaps for different applications. Or, a pseudo-op can be used to manipulate presentation of a program to make it easier to read and maintain. Another common use of pseudo-ops is to reserve storage areas for run-time data and optionally initialize their contents to known values. Directives Used to declare code, data areas, select memory model, declare procedures, etc. In MASM, directives are not case sensitive (.data, .DATA, and .Data are equivalent) Different assemblers have different directives (NASM is not the same as MASM) MASM (Microsoft Assembler) versus NASM (Netwide Assembler)

Example The DWORD directive in the code below tells the assembler to reserve space in the program for a doubleword variable. The MOV instruction will execute at runtime and will copy the contents of myVar to theEAX register (A 32-bit general purpose register). myVar DWORD, 26 ; DWORD directive, stores a 26 in myVar mov eax, myVar ; MOV instruction 1.6 Instructions Instructions are assembled into machine code by the assembler They are executed at runtime by the CPU We will be using the IA-32 instruction set in this course An instruction contains Label (Optional) Mnemonic (required) Operand (depends on the instruction) Comment (optional, noted by a semicolon) For example: L1: mov

ax,bx ; mov the contents of the bx reg into the ax register 4

Labels These act as place markers (Mark the address (offset) of code and data They follow the identifier rules Data labels must be unique. myArray would be an example of a data label Code label is the target of jump and loop instructions L1: jmp L1 ;unconditional jump to label L1

Mnemonics An instruction mnemonic is a short word that identifies an instruction mov Move or assign one value to another add Add two value sub Subtract one value from another Operands Assembly language instructions can have anywhere between 0 and 3 operands Each operand can be a register, a memory operand, constant expression, or an input/output port mov count, ebx ; Has two operands, moves ebx to variable count

Comments Single line comments begin with a semicolon (;) Block comments begin with the COMMENT directive and a user specified symbol. All subsequent lines of text are ignored by the assembler until the same user specified symbol appears COMMENT ! This line is ignored This line is ignored ! ; Or you could use the ampersand symbol COMMENT & This line is ignored This line is ignored & Instruction Format Examples NOP ;This is the no operation instruction/ Takes up 1 byte of storage and does nothing. Instructions with no operands such as: stc

;set the carry flag

Instructions with 1 operands inc eax ; increment the contents of the eax register inc myByte ;increment the contents of the memory location myByte Instructions with 2 operands add ebx, ecx ; add ecx to ebx sub myByte, 25 ; subtract 25 from myByte

5

2 Adding and Subtracting 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 (Kips Library call) call WaitMsg ; Displays Press any key to continue and waits for key to be pressed exit main ENDP END main Program Output, showing registers and flags EAX = 00030000 CF=0 SF=0 OF=0 (Carry Flag, Sign Flag, Overflow Flag)

3 Coding Standard and Example Programs • • • • • • • • •

descriptive identifier names spaces surrounding arithmetic operators blank lines between procedures code and data labels – no indentation executable instructions – indent 4-5 spaces comments: right side of page, aligned vertically 1-3 spaces between instruction and its operands ex: mov ax,bx 1-2 blank lines between procedures

For your program always put the bolded items in your code and use main as noted below so I can test your programs more easily We will be using the Irvine Libraries in this class as in 3.1 below. 3.1 Add and subtract 32-bit Integers Example Using the Irvine Libs ; John Smith ; 1/24/14 ; This program adds and subtracts 32-bit integers ; and stores the sum in a variable. INCLUDE Irvine32.inc .data val1 dword 10000h val2 dword 40000h val3 dword 20000h finalVal dword ? .code main PROC 6

mov add sub mov call exit main ENDP END main

eax,val1 eax,val2 eax,val3 finalVal,eax DumpRegs

; start with 10000h ; add 40000h ; subtract 20000h ; store the result (30000h) ; display the registers

3.2 Add and subtract 32-bit Integers Example Using No Irvine Libs ;The TITLE below allows you to put a name for your program TITLE Add and Subtract (AddSubAlt.asm) ; This program adds and subtracts 32-bit integers. .386 .MODEL flat,stdcall .STACK 4096 ExitProcess PROTO, dwExitCode:DWORD DumpRegs PROTO .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs INVOKE ExitProcess,0 main ENDP END main 3.3 The Basic Program Template for your programs will be TITLE Program Template (Template.asm) ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine32.inc .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedures here) END main

7

4 Assembling, Linking, and Running Programs • 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. Link Library Source File

Step 2: assembler

Step 1: text editor

Object File Listing File

Step 3: linker

Step 4:

Executable OS loader Output File Map File

We will be using Visual Studio 2010 or 2012 to assemble and link our programs You will need to follow CPSC 240 - Lecture 4 - Getting Started with MASM and Visual Studio 2012(KipIrvine)-EricVer1.0(Works) or the version for 2010 to create a project file for your assembly programs. 4.1 A Listing File As you following the directions above, you will see that the project file will be generating a listing file. But what is a listing file and will I need to use it. Since you are using the debugger in VS2010 or 2012, you probably will not need to use the .lst file. • Use it to see how your program is compiled • Contains • source code • addresses • object code (machine language) • segment names • symbols (variables, procedures, and constants) The partial listing example below is from AddSub.lst located in Irvine Examples, Chpt 3 • • • • • • • • • • • • •

Microsoft (R) Macro Assembler Version 8.00.50727.42 *Add and Subtract (AddSub.asm

01/28/06 17:52:13 Page 1 - 1

TITLE Add and Subtract

(AddSub.asm)

; This program adds and subtracts 32-bit integers. ; Last update: 2/1/02 INCLUDE Irvine32.inc C ; Include file for Irvine32.lib C C ;OPTION CASEMAP:NONE

; optional: make identifiers

C C INCLUDE SmallWin.inc

; MS-Windows prototypes,

(Irvine32.inc)

case-sensitive • • structures, and constants • • • •

C .NOLIST C .LIST C C INCLUDE VirtualKeys.inc

8

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

C C C C C C C C 00000000 00000000 00000000 00000005 0000000A 0000000F

; VirtualKeys.inc .NOLIST .LIST

.NOLIST .LIST

.code main PROC B8 05 2D E8

00010000 00040000 00020000 00000000 E

mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs

exit push +000000000h * call ExitProcess main ENDP END main _Microsoft (R) Macro Assembler Version 8.00.50727.42 *Add and Subtract (AddSub.asm 00000014 00000016 0000001B

; EAX = 10000h ; EAX = 50000h ; EAX = 30000h

6A 00 * E8 00000000 E

01/28/06 17:52:13 Symbols 2 - 1

Structures and Unions: N a m e

Size Offset

CONSOLE_CURSOR_INFO . . . . dwSize . . . . . . . . . . bVisible . . . . . . . . . CONSOLE_SCREEN_BUFFER_INFO . dwSize . . . . . . . . . . dwCursorPosition . . . . . wHour . . . . . . . . . . . wMinute . . . . . . . . . wSecond . . . . . . . . . wMilliseconds . . . . . . WINDOW_BUFFER_SIZE_RECORD . dwSize . . . . . . . . . . ; Again, there is a lot more

Type

. . 00000008 . . 00000000 DWord . . 00000004 DWord . . 00000016 . . 00000000 DWord . . 00000004 DWord . 00000008 Word . . 0000000A Word . . 0000000C Word . . 0000000E Word . . 00000004 . . 00000000 DWord in the complete listing

Segments and Groups: N a m e FLAT . STACK _DATA _TEXT

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

Size . . . .

. . . .

. . . .

. . . .

GROUP 32 Bit 32 Bit 32 Bit

Length

Align

00001000 Para 00000000 Para 0000001B Para

Stack Public Public

Procedures, parameters, and locals: N a m e

Type

9

Value

Combine Class

Attr

'STACK' 'DATA' 'CODE'

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

CloseFile . STDCALL CloseHandle STDCALL Clrscr . . . STDCALL ; There is a

. . . . . . . . . .

P Near

00000000 FLAT

Length= 00000000 External

. . . . . . . . . .

P Near

00000000 FLAT

Length= 00000000 External

. . . . . . . . . .

P Near

00000000 FLAT

Length= 00000000 External

lot more in the complete listing

Symbols: N a m e @CodeSize . . . . . . . . . @DataSize . . . . . . . . . @Interface . . . . . . . . . @Model . . . . . . . . . . . @code . . . . . . . . . . . @data . . . . . . . . . . . @fardata? . . . . . . . . . @fardata . . . . . . . . . . @stack . . . . . . . . . . . ALT_MASK . . . . . . . . . . CAPSLOCK_ON . . . . . . . . CREATE_ALWAYS . . . . . . . CREATE_NEW . . . . . . . . . CTRL_MASK . . . . . . . . . CreateFile . . . . . . . . . DO_NOT_SHARE . . . . . . . . ENABLE_ECHO_INPUT . . . . . ENABLE_LINE_INPUT . . . . . ENABLE_MOUSE_INPUT . . . . . ; Again, there is a lot more 0 Warnings 0 Errors

Type

Value

Attr

. . Number 00000000h . . Number 00000000h . . Number 00000003h . . Number 00000007h . . Text _TEXT . . Text FLAT . . Text FLAT . . Text FLAT . . Text FLAT . . Number 00000003h . . Number 00000080h . . Number 00000002h . . Number 00000001h . . Number 0000000Ch . . Text CreateFileA . . Number 00000000h . . Number 00000004h . . Number 00000002h . . Number 00000010h in the complete listing

4.2 Map files Information about each program segment: • starting address • ending address • size • segment type • An example might be addSub.map (Generated when you build your program)

5 Defining Data • • • • • • • • •

Intrinsic Data Types Data Definition Statement Defining BYTE and SBYTE Data Defining WORD and SWORD Data Defining DWORD and SDWORD Data Defining QWORD Data Defining TBYTE Data Defining Real Number Data Little Endian Order 10

• •

Adding Variables to the AddSub Program Declaring Uninitialized Data

5.1 •

Intrinsic Data Types BYTE...


Similar Free PDFs