Text of Assembly Language Assembly Language Fundamentals
Slide 1
Assembly Language Assembly Language Fundamentals
Slide 2
Basic Elements Directives Embedded in the source code that is recognized and acted upon by the assembler Do not execute at run time Define variables, macros, and procedures For example TITLE INCLUDE .CODE PROC ENDP END
Slide 3
Instructions Translated by the assembler into machine language bytes, which are loaded and executed by the CPU at run time. Format: [label] mnemonic operand(s) [;comment] For example: L1: mov ax, bx ; copy data from bx to ax mov eax, 10000h call DumpRegs
Slide 4
Integer Constants Format: [{+|-}] digits [radix] Radix: h, Hexadecimal q/o, Octal d/t, Decimal b/y, Binary r, Encoded real For example: 26Decimal 26dDecimal 11010011bBinary 42qOctal 42oOctal 1AhHexadecimal 0A3hHexadecimal, note the 0 before A
Slide 5
Integer Expressions Use (, ), +, -, *, /, MOD Precedence: (, ) > +, - (unary) > *, / > MOD > +, - For example: 16/5value = 3 -(3+4) * (6-1)value = -35 -3 + 4*6 -1value = 20 25 mod 3value = 1 Note: The integer expressions are not instructions. They are processed by the assembler, but not executed by the CPU at the running time
Slide 6
Real Number Constants Format: [{+,-}]integer.[integer][exponent] exponent: E[{+,-}]integer For example: 2. +3.0 -44.2E+05 26.E5
Slide 7
Character Constants Enclosed in single or double quotes For example: A d String Constants Enclosed in single or double quotes For example: ABC Good night, Gracie This isn t a test Say Good night, Gracie Note: Not like C, null byte is not automatically added after the double quotes
Slide 8
Reserved Words Instruction mnemonics, such as MOV, ADD, MUL. Directives Attributes providing size and usage information for variables and operands, such as BYTE and WORD. Operators Predefined symbols, such as @data.
Slide 9
Identifiers Contain between 1 and 127 characters Not case sensitive The first character must be a letter (A..Z, a..z), underscore (_), @, ?, or $. Cannot be reserved words. For example: var1 $first _main open_file @@myfile _12345
Slide 10
Label An identifier that acts as a place marker for instruction or data Data label, for example count DWORD 100 array DWORD 1024, 2048 DWORD 4096, 8192 Code label L1: mov ax, bx ; copy data from bx to ax
Slide 11
Comments Single-line comments, beginning with a semicolon character (;) Block comments, beginning with the COMMENT directive and a user-specified symbol and with the same user-specified symbol For example: COMMENT ! This line is a comment This line is also a omment ! COMMENT & This line is a comment This line is also a omment &
Slide 12
NOP instruction Takes up 1 byte of program storage and doesn t do any work For example: mov ax, bx nop mov edx, ecx
Slide 13
Example: Adding Three Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 INCLUDE Irvine32.inc .code main PROC moveax,10000h; EAX = 10000h addeax,40000h; EAX = 50000h subeax,20000h; EAX = 30000h callDumpRegs exit main ENDP END main
Slide 14
TITLE Add and Subtract (AddSub.asm) The TITLE directive marks the entire line as a comment ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 Comments can be put after a semicolon 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.
Slide 15
main PROC The PROC directive identifies the beginning of a procedure. The name of the procedure here is main. moveax,10000h; EAX = 10000h The MOV instruction copies the integer 10000h to the EAX register. addeax,40000h; EAX = 50000h The ADD instruction adds 40000h to the EAX register. subeax,20000h; EAX = 30000h The SUB instruction subtracts 20000h from the EAX register. callDumpRegs The CALL instruction calls a procedure DumpRegs.
Slide 16
exit The exit macro (indirectly) calls a predefined MS-Windows function that halts the program 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.
Slide 17
TITLE Add and Subtract (AddSubAlt.asm) ; This program adds and subtracts 32-bit integers. ; 32-bit Protected mode version ; Last update: 06/01/2006 .386 .MODEL flat,stdcall .STACK 4096 ExitProcess PROTO,dwExitCode:DWORD DumpRegs PROTO .code main PROC moveax,10000h; EAX = 10000h addeax,40000h; EAX = 50000h subeax,20000h; EAX = 30000h callDumpRegs INVOKE ExitProcess,0 main ENDP END main Alternative Version of AddSub
Slide 18
.386 The.386 directive identifies the minimum CPU required for this program .MODEL flat,stdcall The. MODEL directive instructs the assembler to generate code for a protected mode program, and STDCALL enables the calling of MS-Windows functions .STACK 4096 Reserve 4086 bytes of stack space
Slide 19
ExitProcess PROTO,dwExitCode:DWORD DumpRegs PROTO Two PROTO directives declare prototypes for procedures used by this program. ExitProcess is an MS-Windows function. DumpRegs is a procedure from the Irvine32 link library INVOKE ExitProcess,0 INVOKE is an assembler directive that calls a procedure or function
Slide 20
Progrm Template TITLE Program Template(template.asm) ; Program Description: ; Author: ; Date Created: ; Last Modification Date: INCLUDE Irvine32.inc ; (insert symbol definitions here) .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit; exit to operating system main ENDP ; (insert additional procedures here) END main
Slide 21
Assembling, Linking, and Running Programs
Slide 22
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, 2007. 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.
Slide 23
Listing File Microsoft (R) Macro Assembler Version 6.15.8803 09/27/06 22:12:24 Add and Subtract (AddSub.asm) Page 1 - 1 TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. ; Last update: 06/01/2006 INCLUDE Irvine32.inc C ; Include file for Irvine32.lib (Irvine32.inc) C
Slide 24
C INCLUDE SmallWin.inc ; MS-Windows prototypes, structures, and constants C.NOLIST C.LIST C C.NOLIST C.LIST C 00000000.code 00000000main PROC 00000000 B8 00010000mov eax,10000h; EAX = 10000h 00000005 05 00040000add eax,40000h; EAX = 50000h 0000000A 2D 00020000sub eax,20000h; EAX = 30000h 0000000F E8 00000000 EcallDumpRegs exit 0000001Bmain ENDP END main
Slide 25
Microsoft (R) Macro Assembler Version 6.15.8803 09/27/06 22:12:24 Add and Subtract (AddSub.asm) Symbols 2 - 1 Structures and Unions: N a m e Size Offset Type CONSOLE_CURSOR_INFO...... 00000005 dwSize............ 00000000 DWord bVisible........... 00000004 Byte CONSOLE_SCREEN_BUFFER_INFO... 00000016 dwSize............ 00000000 DWord dwCursorPos......... 00000004 DWord wAttributes......... 00000008 Word srWindow........... 0000000A QWord maxWinSize.......... 00000012 DWord COORD............. 00000004 X.............. 00000000 Word Y.............. 00000002 Word
Slide 26
FILETIME............ 00000008 loDateTime.......... 00000000 DWord hiDateTime.......... 00000004 DWord SMALL_RECT........... 00000008 Left............. 00000000 Word Top............. 00000002 Word Right............ 00000004 Word Bottom............ 00000006 Word SYSTEMTIME........... 00000010 wYear............ 00000000 Word wMonth............ 00000002 Word wDayOfWeek.......... 00000004 Word wDay............. 00000006 Word wHour............ 00000008 Word wMinute........... 0000000A Word wSecond........... 0000000C Word wMilliseconds........ 0000000E Word
Slide 27
Segments and Groups: N a m e Size Length Align Combine Class FLAT..............GROUP STACK.............32 Bit 00001000 DWord Stack 'STACK' _DATA.............32 Bit 00000000 DWord Public 'DATA' _TEXT.............32 Bit 0000001B DWord Public 'CODE' Procedures, parameters and locals: N a m e Type Value Attr CloseHandle..........P Near 00000000 FLAT Length= 00000000 External STDCALL ClrScr.............P Near 00000000 FLAT Length= 00000000 External STDCALL CreateFileA..........P Near 00000000 FLAT Length= 00000000 External STDCALL
Slide 28
Crlf..............P Near 00000000 FLAT Length= 00000000 External STDCALL Delay.............P Near 00000000 FLAT Length= 00000000 External STDCALL DumpMem............P Near 00000000 FLAT Length= 00000000 External STDCALL DumpRegs............P Near 00000000 FLAT Length= 00000000 External STDCALL ExitProcess..........P Near 00000000 FLAT Length= 00000000 External STDCALL FlushConsoleInp