Docsity

Machine Code and Assemblers - Lecture Slides | CSC 246, Study notes of Computer Science

Partial preview of the text

Download Machine Code and Assemblers - Lecture Slides | CSC 246 and more Study notes Computer Science in PDF only on Docsity!

Machine Code and AssemblersMachine Code and Assemblers

November 6November 6

CSC201 Section 002

Fall, 2000

Definitions Definitions

  • Assembly time vs. link time vs. load time vs. run time

.C file

.ASM file

.OBJ file

.EXE file

Running program

compiler

assembler

linker

Loader (O.S.)

Tasks of the Assembler Tasks of the Assembler

  • Macro expansion (preprocessing)
  • Organize memory, reserve and initialize memory for data
  • Convert instructions and constants into binary
  • Convert symbolic addresses into absolute addresses
  • Mark external references for later resolution

Memory Organization Memory Organization

  • Separate memory "regions" for the stack, the code, and the data
  • Memory is allocated sequentially as each part of the program is processed - The assembler keeps track of how much has been allocated so far

Code

stack

data

Example memory organization

The Symbol Table The Symbol Table

  • For code and data, keep track of...
    • Memory address corresponding to that symbolic name, determined during memory allocation
  • For data, keep track of...
    • Data type (byte? word? doubleword? floating-point?)
    • Initial value
    • Size (amount of memory)
  • Table has no use after assembly

Directives Directives

  • Provide information to the assembler, used to generate correct output
  • "title" -- no processing
  • ".486" -- check for 486-compatible instruction syntax, and generate 486 machine code
  • ".model flat" -- use the flat (not segmented) memory model

Compiling C to Assembler Compiling C to Assembler

  • The user's program will be called by the operating system like a procedure - On entry, save and initialize EBP, save registers - For this example, assumes local storage has been allocated on the stack before the program is called - On exit, restore registers and execute "ret" statement

x86 Machine Code x86 Machine Code

  • Although assembly languages on machines are similar, machine languages are completely different
  • Pentium machine code "peculiarities"
    • Variable length instruction encodings
    • Many addressing modes
    • Extra byte needed to specify addressing mode in some cases
    • "Repeat" prefixes
  • None of these commonly used today

Instruction Formats Instruction Formats

  • Motivation: understand slightly how CPU "reads" an executable program
  • Goals for instruction format design (conflicting): use minimum space, vs. make decoding as easy as possible
  • x86 encoding: complex, irregular, baroque even
  • All instruction encodings have an "opcode" specifier
  • For the most part, we are looking at 32-bit operand versions only

Register Specification Register Specification

  • We’re looking at doubleword-length registers only
  • Note: not in order!

111 EDI

110 ESI

101 EBP

100 ESP

011 EBX

010 EDX

001 ECX

000 EAX

Binary Symbol Register

D, V, W D, V, W

  • D: 0= "reg" field is source, 1= "reg" field is destination
  • V: 0= shift by 1 bit, 1= shift by amount in cl register
  • W: 0= data is byte length, 1= data is word length

Encoding Instructions Using Encoding Instructions Using

Appendix CAppendix C

Conditional Jump Conditional Jump

  • Conditional jump instruction encodes displacement from next instruction - 32-bit offset for our purposes