Chapter 3: Assembly Language Fundamentals
2 Chapter Overview • Basic Elements of Assembly Language • Example: Adding and Subtracting Integers • Assembling, Linking, and Running Programs • Defining Data • Symbolic Constants • Real-Address Mode Programming
3 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
4 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
Integer Expressions • Operators and precedence levels: • Examples: 5
6 Character and String Constants • Enclose character in single or double quotes • 'A', "x" • ASCII character = 1 byte • Enclose strings in single or double quotes • "ABC" • 'xyz' • Each character occupies a single byte • Embedded quotes: • 'Say "Goodnight," Gracie'
7 Reserved Words and Identifiers • Reserved words (Appendix D) cannot be used as identifiers • Instruction mnemonics, directives, type attributes, operators, predefined symbols • Identifiers • 1-247 characters, including digits • not case sensitive • first character must be a letter, _, @, ?, or $
8 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. • not case sensitive • Different assemblers have different directives • NASM not the same as MASM, for example
9 Instructions • Assembled into machine code by assembler • Executed at runtime by the CPU • We use the Intel IA-32 instruction set • An instruction contains: • Label • Mnemonic • Operand • Comment (optional) (required) (depends on the instruction) (optional)
10 Labels • Act as place markers • marks the address (offset) of code and data • Follow identifer rules • Data label • must be unique • example: myArray (not followed by colon) • Code label • target of jump and loop instructions • example: L1: (followed by colon)
11 Mnemonics and Operands • Instruction Mnemonics • memory aid • examples: MOV, ADD, SUB, MUL, INC, DEC • Operands • constant • constant expression • register • memory (data label) Constants and constant expressions are often called immediate values
12 Comments • Comments are good! • explain the program's purpose • when it was written, and by whom • revision information • tricky coding techniques • application-specific explanations • Single-line comments • begin with semicolon (;) • Multi-line comments • begin with COMMENT directive and a programmer- chosen character • end with the same programmer-chosen character
13
14
15 Instruction Format Examples • No operands • stc • One operand • inc eax • inc myByte • Two operands • add ebx,ecx • sub myByte,25 • add eax,36 * 25 ; set Carry flag ; register ; memory ; register, register ; memory, constant ; register, constant-expression
Example: Adding and Subtracting Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc .code main PROC 16 mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h ; display registers call DumpRegs exit main ENDP END main
Example Output Program output, showing registers and flags: EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 17
18 Suggested Coding Standards (1 of 2) • Some approaches to capitalization • capitalize nothing • capitalize everything • capitalize all reserved words, including instruction mnemonics and register names • capitalize only directives and operators • Other suggestions • descriptive identifier names • spaces surrounding arithmetic operators • blank lines between procedures
19 Suggested Coding Standards (2 of 2) • Indentation and spacing • code and data labels – no indentation • executable instructions – indent 4-5 spaces • comments: begin at column 40-45, aligned vertically • 1-3 spaces between instruction and its operands • ex: mov ax,bx • 1-2 blank lines between procedures
Alternative Version of AddSub 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 20 ; EAX = 10000h ; EAX = 50000h ; EAX = 30000h .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs INVOKE ExitProcess,0 main ENDP END main
Program Template TITLE Program Template 21 (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
22
23

Lecture 13. Assembly Language of computer

  • 1.
    Chapter 3: AssemblyLanguage Fundamentals
  • 2.
    2 Chapter Overview • BasicElements of Assembly Language • Example: Adding and Subtracting Integers • Assembling, Linking, and Running Programs • Defining Data • Symbolic Constants • Real-Address Mode Programming
  • 3.
    3 Basic Elements ofAssembly Language • Integer constants • Integer expressions • Character and string constants • Reserved words and identifiers • Directives and instructions • Labels • Mnemonics and Operands • Comments • Examples
  • 4.
    4 Integer Constants • Optionalleading + 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
  • 5.
    Integer Expressions • Operatorsand precedence levels: • Examples: 5
  • 6.
    6 Character and StringConstants • Enclose character in single or double quotes • 'A', "x" • ASCII character = 1 byte • Enclose strings in single or double quotes • "ABC" • 'xyz' • Each character occupies a single byte • Embedded quotes: • 'Say "Goodnight," Gracie'
  • 7.
    7 Reserved Words andIdentifiers • Reserved words (Appendix D) cannot be used as identifiers • Instruction mnemonics, directives, type attributes, operators, predefined symbols • Identifiers • 1-247 characters, including digits • not case sensitive • first character must be a letter, _, @, ?, or $
  • 8.
    8 Directives • Commands thatare 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. • not case sensitive • Different assemblers have different directives • NASM not the same as MASM, for example
  • 9.
    9 Instructions • Assembled intomachine code by assembler • Executed at runtime by the CPU • We use the Intel IA-32 instruction set • An instruction contains: • Label • Mnemonic • Operand • Comment (optional) (required) (depends on the instruction) (optional)
  • 10.
    10 Labels • Act asplace markers • marks the address (offset) of code and data • Follow identifer rules • Data label • must be unique • example: myArray (not followed by colon) • Code label • target of jump and loop instructions • example: L1: (followed by colon)
  • 11.
    11 Mnemonics and Operands •Instruction Mnemonics • memory aid • examples: MOV, ADD, SUB, MUL, INC, DEC • Operands • constant • constant expression • register • memory (data label) Constants and constant expressions are often called immediate values
  • 12.
    12 Comments • Comments aregood! • explain the program's purpose • when it was written, and by whom • revision information • tricky coding techniques • application-specific explanations • Single-line comments • begin with semicolon (;) • Multi-line comments • begin with COMMENT directive and a programmer- chosen character • end with the same programmer-chosen character
  • 13.
  • 14.
  • 15.
    15 Instruction Format Examples •No operands • stc • One operand • inc eax • inc myByte • Two operands • add ebx,ecx • sub myByte,25 • add eax,36 * 25 ; set Carry flag ; register ; memory ; register, register ; memory, constant ; register, constant-expression
  • 16.
    Example: Adding andSubtracting Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc .code main PROC 16 mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h ; display registers call DumpRegs exit main ENDP END main
  • 17.
    Example Output Program output,showing registers and flags: EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0 17
  • 18.
    18 Suggested Coding Standards(1 of 2) • Some approaches to capitalization • capitalize nothing • capitalize everything • capitalize all reserved words, including instruction mnemonics and register names • capitalize only directives and operators • Other suggestions • descriptive identifier names • spaces surrounding arithmetic operators • blank lines between procedures
  • 19.
    19 Suggested Coding Standards(2 of 2) • Indentation and spacing • code and data labels – no indentation • executable instructions – indent 4-5 spaces • comments: begin at column 40-45, aligned vertically • 1-3 spaces between instruction and its operands • ex: mov ax,bx • 1-2 blank lines between procedures
  • 20.
    Alternative Version ofAddSub 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 20 ; EAX = 10000h ; EAX = 50000h ; EAX = 30000h .code main PROC mov eax,10000h add eax,40000h sub eax,20000h call DumpRegs INVOKE ExitProcess,0 main ENDP END main
  • 21.
    Program Template TITLE ProgramTemplate 21 (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
  • 22.
  • 23.