Intel X86 Assembly Fundamentals X86 Assembly Language X86 Assembly

Intel X86 Assembly Fundamentals X86 Assembly Language X86 Assembly

x86 Assembly Language Intel x86 Assembly Fundamentals Fundamentals Comppgzuter Organization and Assembl yggy Languages Yung-Yu Chuang with slides by Kip Irvine Instructions Labels • Assembled into machine code by assembler • Act as place markers – marks the address (offset) of code and data • Executed at runtime by the CPU • Easier to memorize and more flexible • Member of the Intel IA-32 instruction set mov ax, [0020] → mov ax, val • Four parts • Follow identifier rules – Label (optional) • Data lbllabel – Mnemonic (required) – must be unique – Operand (usually required) –example: myArray BYTE 10 – Comment (optional) • Code label (ends with a colon) – target of jump and loop instructions Label: Mnemonic Operand(s) ;Comment –example: L1: mov ax, bx ... 3 jmp L1 4 Reserved words and identifiers Mnemonics and operands • Reserved words cannot be used as identifiers • Instruction mnemonics – Instruction mnemonics, directives, type attributes, – "reminder" operators, predefined symbols –examples: MOV, ADD, SUB, MUL, INC, DEC • Identifiers • Operands – 1-247 characters, including digits – constant (immediate value), 96 – case insensitive (by default) – constant expression, 2+4 – first character must be a letter, _, @, or $ –Register, eax –examples: –memoryy( (data label), coun t var1 Count $first • Number of operands: 0 to 3 _main MAX open_file – stc ; set Carry flag @@myfile xVal _12345 – inc ax ; add 1 to ax – mov count, bx ; move BX to count 5 6 Directives Comments • Commands that are recognized and acted upon • Comments are good! by the assembler – explain the program's purpose – tricky coding techniques – Part of assembler’s syntax but not part of the Intel instruction set –application-spppecific explanations – Used to declare code, data areas, select memory • Single-line comments model, declare procedures, etc. –begin with semicolon (;) – case insensitive • block comments • Different assemblers have different directives – begin with COMMENT directive and a programmer- chosen character and end with the same – NASM != MASM, for example programmer-chosen character • ElExamples: .ddOCdata .code PROC COMMENT ! This is a comment and this line is also a comment 7 ! 8 Example: adding/subtracting integers Example output directive marking a comment TITLE Add and Subtract (AddSub.asm) Program output, showing registers and flags: comment ; This program adds and subtracts 32-bit integers. EAX= 00030000 EBX= 7FFDF000 ECX= 00000101 EDX= FFFFFFFF INCLUDE Irvine32.inc copy definitions from Irvine32.inc ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 .code code segme nt. 3 segme nts: code, data, stack EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 main PROC beginning of a procedure mov eax,10000hsource ; EAX = 10000h add eax,40000h dtitidestination; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display registers exit defined in Irvine32. inc to end a program main ENDP END main marks the last line and define the startup procedure 9 10 Alternative version of AddSub Program template TITLE Add and Subtract (AddSubAlt.asm) TITLE Program Template (Template.asm) ; This program adds and subtracts 32-bit integers. ; Program Description: .386 ; Author: .MODEL flat,stdcall ; Creation Date: .STACK 4096 ; Revisions: ; Date: Modified by: ExitProcess PROTO, dwExitCode:DWORD DumpRegs PROTO .data .code ; (insert variables here) main PROC .code mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h main PROC sub eax,20000h ; EAX = 30000h ; (insert executable instructions here) call DumpRegs exit INVOKE ExitProcess,0 main ENDP main ENDP ; (insert additional procedures here) END main END main 11 12 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,,p Steps 2 throu gh 4 must be repeated. Defining data Link Library Step 2: Step 3: Step 4: Source assembler Object linker Executable OS loader Output File File File Listing Map Step 1: text editor File File 13 Intrinsic data types (1 of 2) Intrinsic data types (2 of 2) • BYTE, SBYTE • REAL4 – 8-bit unsigne d itinteger; 8-bit signe d in teger – 4-btbyte IEEE shor t real • WORD, SWORD • REAL8 – 16-bit unsigned & signed integer – 8-byte IEEE long real • DWORD, SDWORD • REAL10 – 32-bit unsigned & signed integer – 10-byte IEEE extended real • QWORD – 64-bit integer • TBYTE – 80-bit integer 15 16 Data definition statement Integer constants • A data definition statement sets aside storage in • [{+|-}] digits [radix] memory for a variable. • Optional leading + or – sign • May optionally assign a name (label) to the data. • binary, decimal, hexadecimal, or octal digits • Only size matters, other attributes such as signed are • Common radix chtharacters: just reminders for programmers. – h – hexadecimal • Syntax: – d – didecima l (dflt)(default) [name] directive initializer [,initializer] . – b – binary At least one initializer is required, can be ? – r – encoded real • All initializers become binary data in memory – o – octal Examples: 30d, 6Ah, 42, 42o, 1101b Hexadecimal beginning with letter: 0A5h 17 18 Integer expressions Real number constants (encoded reals) • Operators and precedence levels: • Fixed point v.s. floating point 1 8 23 SE M ±1.bbbb×2 (E-127) • Examples: • Example 3F800000r=+1.0,37.75=42170000r • double 111 52 SE M 19 20 Real number constants (decimal reals) Character and string constants •[sign]integer.[integer][exponent] • Enclose character in single or double quotes sign → {|{+|-} – 'A', "x " exponent → E[{+|-}]integer – ASCII character = 1 byte • Examples: •Enclldbllose strings in single or double quotes – "ABC" 2. – 'xyz' +3.0 – Each character occupies a single byte -44.2E+05 26.E5 • Embedded quotes: – ‘Sayyg "Goodnight," Gracie’ – "This isn't a test" 21 22 Defining BYTE and SBYTE Data Defining multiple bytes Each of the following defines a single byte of storage: Examples that use multiple initializers: value1 BYTE 'A‘ ; character constant value 2 BYTE 0 ; sma lles t uns igne d by te list1 BYTE 10, 20, 30, 40 value3 BYTE 255 ; largest unsigned byte list2 BYTE 10,20,30,40 value4 SBYTE -128 ; smallest signed byte BYTE 50,60,70,80 value5 SBYTE +127 ; largest signed byte BYTE 81,,,,82,83,84 value6 BYTE ? ; uninitialized byte list3 BYTE ?,32,41h,00100010b A variable name is a data label that implies an offset list4 BYTE 0Ah,20h,‘A’,22h (an address). 23 24 Defining strings (1 of 2) Defining strings (2 of 2) • A string is implemented as an array of • End-of-line character sequence: characters – 0Dh = carriage return – For convenience, it is usually enclosed in – 0Ah = line feed quotation marks – It usually has a null byte at the end str1 BYTE "Enter your name: ",0Dh,0Ah • Examples: BYTE "Enter your address: ",0 str1 BYTE "Enter your name",0 str2 BYTE 'Error: halting program',0 str3 BYTE 'A','E','I','O','U' newLine BYTE 0Dh, 0Ah, 0 greeting1 BYTE "Welcome to the Encryption Demo program " BYTE "created by Kip Irvine.," 0 greeting2 \ Idea: Define all strings used by your program in BYTE "Welcome to the Encryption Demo program " the same area of the data segment. BYTE "created by Kip Irvine.",0 25 26 Using the DUP operator Defining WORD and SWORD data •Use DUP to allocate (create space for) an array or • Define storage for 16-bit integers string. – or double characters • Counter and argument must be constants or constant – single value or multiple values expressions word1 WORD 65535 ; largest unsigned var1 BYTE 20 DUP(0) ; 20 bytes, all zero word2 SWORD –32768 ; smallest signed var2 BYTE 20 DUP(?) ; 20 bytes, word3 WORD ? ; uninitialized, ; unsigned ; unin itia lize d word4 WORD "AB" ; double characters var3 BYTE 4 DUP("STACK") ; 20 bytes: myList WORD 1, ,,,,2,3,4,5 ;;y array of words ;"STACKSTACKSTACKSTACK" array WORD 5 DUP(?) ; uninitialized array var4 BYTE 10, 3 DUP(0), 20 27 28 Defining DWORD and SDWORD data Defining QWORD, TBYTE, Real Data Storage definitions for quadwords, tenbyte values, Storage definitions for signed and unsigned 32-bit and real numbers: integers: qqQuad1 QWORD 1234567812345678h val1 DWORD 12345678h ; unsigned val1 TBYTE 1000000000123456789Ah val2 SDWORD –2147483648 ; signed rVal1 REAL4 -2.1 val3 DWORD 20 DUP(?) ; unsigned array rVal2 REAL8 3.2E-260 val4 SDWORD –3,–2,–1011,0,1 ; signed array rVal3 REAL10 4.6E+4096 ShortArray REAL4 20 DUP(0.0) 29 30 Little Endian order Adding variables to AddSub • All data types larger than a byte store their TITLE Add and Subtract, (AddSub2.asm) individual bytes in reverse order. The least INCLUDE Irvine32. inc .data significant byte occurs at the first (lowest) val1 DWORD 10000h memory address. val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? • Example: .code main PROC val1 DWORD 12345678h mov eax,val1 ; start with 10000h add eax,val2 ; add 40000h sub eax,val3 ; subtract 20000h mov finalVal,eax ;;() store the result (30000h) call DumpRegs ; display the registers exit main ENDP END main 31 32 Declaring unitialized data Mixing code and data •Use the .data? directive to declare an .code unitilidintialized dtdata segment: mov eax, ebx .data? .data temp DWORD ? • Within the segment, declare variables with "?" .code initializers: (will not be assembled into .exe) mov temp, eax Advantage: the program's EXE file size is reduced. .data smallArray DWORD 10 DUP(0) .data? bigArray DWORD 5000 DUP(?) 33 34 Equal-sign directive • name = expression –expression is a 32-bit in teger (i(expression or constt)tant) – may be redefined Symbolic constants – name is calldlled a symbo lic constan t • good programming style to

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    18 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us