Chapter 5 Assembly Language Programming Purpose: Introduction



Download 24.16 Kb.
Date09.01.2017
Size24.16 Kb.
Chapter 5 Assembly Language Programming
Purpose:
Introduction:

  1. Memory address: byte-addressable vs. word-addressable

  2. Instructions: fixed length vs. variable length

  3. Data and instruction share the main memory

  4. Questions: Word size? Main memory size? How to determine the size of an instruction in variable length instruction set? How data and instructions are fetched from the main memory?




  1. Instruction sequencing: sequential execution, branch and jump, procedure call and return

  2. Four classes of instructions: Data transfer between main memory and registers; arithmetic and logic operations; Sequencing instructions; and data transfer between main memory and I/O devices.

  3. Instruction cycle: IF, ID, increment PC, fetch data, do operation, store result, go back.

  4. Questions: After a word is fetched, how can you tell whether it is an instruction or a data word?




  1. Register transfer notation: general-purpose registers (R0 – R31), dedicated registers (PC, MAR, MDR) , Memory (M); Square brackets denote the content of a register; arrows denote transfers

  2. Assembly language notation: Opcode Destination, Source1, Source2


Instruction Format:

  1. Each instruction contains three parts: Opcode, modifiers, operand addresses

  2. Operand addresses can be registers or memory locations




  1. Example basic instructions: add, load, store, and move

  • Addition: register-register (load-store computer), memory-memory, register-memory

  • Load from main memory to register

  • Store from register to main memory

  • Move data




  1. Instruction format:

  • Four-address format:
    SEAC, 1945, first computer with a stored program capability. Word-addressable with a word length of 46bits. Manipulate with octal numbers.

    How many memory words in SEAC? What does stored program mean?



    Example: Write a program to find out when the artillery shell hits the ground.
    , initially ; .



  • Three-address format – modified SEAC
    No D address, need PC
    12 bits per address

    Questions: How many words the computer can address? Which instructions need to be modified? And how?

    General-purpose register (register-register, load-store) computer falls this category.



  • Two-address format – modify SEAC again to reduces its addresses to two
    6bytes per instruction, 18 bits per address, much larger memory

    Question: how to avoid the overwritten of A in A [A]+[B]?

    Many transistorized computers used a two-address format with the assistance of ACC as the register for the operation result, thus eliminating the data transfer to retain the value in A.





  • One-address format: only one address A remains. The other operand comes from ACC; result goes to ACC.



  • Branching and Condition Codes: Instead of using compp and compn, many computers use condition codes in a Status register that specify the state of the result of the last operation. Typical condition codes are: N, Z, V, C

    Branch positive, branch negative, etc





Addressing Modes:

  1. specify how a computer can access its operands and store the result in its registers and main memory.

  2. Immediate addressing: operand is given as part of the instruction, suitable for small integers for ALU; eg. Addi

  3. Register Addressing: The address of an operand is a register.

  4. Direct Addressing: also called absolute addressing. The first method used by electronic stored-program computers. The address in main memory is included in the instruction to obtain data. Any shortcomings?

  5. Indirect Addressing: the content of a register is the main memory address of the operand. Eg. LDW R1, (R2)
    Example: Sum integers. Page 12. Steps: register allocation; initialization; do the job; store result.



  6. Indexing and Arrays: Indexing is a technique that allows a value to be added to the content of a register (called index register) to form an effective address.
    Eg. ADD R3, C(R2)  EA = C+[R2], C is called displacement or offset
    Example applications:
    C can hold the address of the first element in an array and the index register is initialized to 0 and then index into the array.
    Eg. ADD R3, (R1)(R2)
    Example:
    re-do the example of summing integers.
    The modifier field in SEAC machine.



  7. Base and Indexing Addressing:

  8. Base, Index, and Offset Addressing:



  9. Relative Addressing: EA = [PC]+displacement for target address computation.



  10. Autoincrement and Autodecrement: Designed for push and pop in stack operation
    Eg. (Rx)+ for autoincrement and –(Rx) for autodecrement.



The Assembler
Definition: Assembler is a program that translates the assembly code into machine code.


The structure of a sentence in an assembly language has 4 parts:

  • A label: optional, stands for the name of the location of an instruction of a piece of data
    eg. Msg1: .string “Hello World!”

  • A mnemonic: stands for the operation

  • Addresses for the operands and result: register number, or memory address, or a constant

  • Comment: optional.

The set of rules that define each instruction type and how it can be used is called the syntax of the assembly language.



  • Syntax error!


How does an assembler translate an assembly code into object code?

Two-pass assembler as an example.



  1. Find out the label(variable)-address pairs (insert them into a symbol table); changes the operation to the binary op code; change decimal numbers to binary, etc.

  2. Do the translation based on instruction format to get the machine code.


Question: How to assemble external variables? (eg. printf in c)

Answer: Seek the help of linker.
Directives: direct/assist the assembler to do its job (to understand the desire of the programmer)! (start, text, data, etc)

Directives are reserved words! They are not instructions!


Possible Directives for an assembler:
Label Directives Operand

B EQU 300

ORIGIN 420

LIST RESERVE 320

N DATAWORD 200

JAKE DATABYTE 15

START

DATA:


TEXT:

RETURN


END
Example: Sum the N integers (Page 20-22)

  • Syntax of the instructions  get the instruction format

  • Instruction description  for coding

  • Coding

    • Assign and initialize registers

    • Do the job and then store the result.


The linker and Loader

Linker is used to resolve external references



Loader is used to load the executable into the main memory and assign a new address to each variable/label. Why?
Typical Instructions
Integer Arithmetic Instructions: +,-, *, /; addressing mode, HI and LO registers for mul and div, etc.
Floating-Point Arithmetic Instructions: +,-, *, /, and/or sqrt. Trigonometric functions are done through subprograms using a technique called numerical analysis.
Decimal Arithmetic Instructions: use BCD code
Data Conversion Instructions: integer to/from FP
Shifting Instructions: logical/arithmetic shift, to do multiplication, etc
Logic Instructions: and, or, not, xor, etc. used to isolate a given set of bits in a register. To set values, etc.
Word and Byte Transmission Instructions: LDW/STW, LDB/STB, etc.
Control and Branch Instructions: comparison based or condition code based; A control instruction is a compare: set the condition code based on comparison result.
Sense Indicators and Status Instructions: bits in the status register! Test and set (TST #n Rx).
Input/Output
Data transfer between the main memory and I/O devices is controlled by OS. Why? – conflict avoidance!
Large mainframe computers use I/O channels for data movement. Smaller computers have simple I/O paths – our focus!
There exist two methods of performing I/O in small computers: program-controlled I/O and memory-mapped I/O.

  • Program-controlled I/O:

    • Need synchronization – the speed of I/O devices and the speed of the main memory are different

    • Buffer registers and status registers residing in the control unit of the device: DIN and DOUT, SIN and SOUT

    • Eg. Type a character from the keyboard and display a character in the monitor.

    • SPECIAL BRANCH instruction is needed to perform the job:
      WAITIN BIN WAITIN
      STB Constant(R3), DIN
      WAITOUT BOUT WAITOUT
      LDB Constant(R3), DOUT
      Efficiency: -- the speed of the computer is slowed down to that of the device!




  • Memory-mapped I/O

    • Computer does not need to know DIN and DOUT. Each I/O device buffer is wired through an interface to move data to or from a particular location in main memory.

    • DIN is mapped to the special location DATAIN in the main memory and DOUT is mapped to the special location DATAOUT in the main memory. These locations are reserved by OS and can not be modified by the user.
      WAITIN BIN WAITIN
      STB DATAIN
      WAITOUT BOUT WAITOUT
      LDB DATAOUT



    • Special BRANCH instructions such as BIN and BOUT can be avoided by using TST instruction and the status register.
      WAITIN TST #3, R1
      BEZ R1, WAITIN
      STB DATAIN
      WAITOUT TST #4, R1
      BEZ R1, WAITOUT
      LDB DATAOUT



    • No efficient in CPU usage again!



  • Computer checks the status either by polling the status register or it is interrupted by the I/O devices if interrupt-driven method is applied.



Stacks
Stack grows from high address to low address

TOS and BOS are well-defined. Initially SP points to the address just above BOS. SP always point to TOS

Stack implements LIFC method of operation

How to implement PUSH and POP operations? What if the machine supports autoincrement and autodecrement addressing?



Queues
Queue is a data structure that implements FIFC.
Subroutines

Definition: A subroutine is a set of instructions that will be used multiple times by a program over different data . A subroutine usually has a name (label), which indicates the location of the subroutine program in the main memory.
To use a subroutine, we need an instruction CALL. The subroutine is the called program (callee) and the main program is the caller program (caller).
Procedure call is a special branch operation, in which the control will be transferred from the caller to the callee. The callee needs to return the control back the caller when its job is finished. This is done by an instruction called RETURN.
Subroutine linkage protocol: Caller saves the return address in the link register and then makes a procedure call. Callee branches to the address stored in the link register when done.
Nested subroutines: subroutine A calls subroutine B, etc,

  • In nested subroutine calling, return address will be overwritten! – stack.

  • Who will save and when to save the return address into the stack? – callee at the beginning.

  • Callee needs to save registers that will be locally used by the subroutine; or both callee and caller can do part of the saving!


Parameter passing: calling parameters and returned parameter are passed by registers or by the stack.

  • Turn the integer sum example into a procedure (subroutine).


Procedure call summary: -- six steps:

  • Pass parameters to callee

    • Register or stack

  • Transfer control to the procedure

    • Call procName

  • Allocate storage resources

    • Save return address register and locally used registers into the stack

  • Perform the desired task

  • Return value

  • Transfer control to the caller

    • return


Stack frame: The space on the stack that is used to pass parameters and save/restore registers is called a stack frame. The frame pointer for a particular subroutine remains fixed throughout the invocation of that instance of the subroutine.

      • Frame pointer points to the address that saves the frame pointer of the caller!

      • Use frame pointer to access data within the stack frame!

      • FP is not a must!

      • Examples!


Download 24.16 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2020
send message

    Main page