Docsity

Assembly language lecture 1, Lecture notes of Assembly Language Programming

Partial preview of the text

Download Assembly language lecture 1 and more Lecture notes Assembly Language Programming in PDF only on Docsity!

Computer Organization and

Assembly Language

Lecture 9 – 10

Outline

  • (^) Indirect addressing
  • (^) Indirect Operand
  • (^) Pointer
  • (^) JMP and LOOP instruction
  • (^) Summing an integer array
  • (^) Copying a string
  • (^) Practice Problems

Indirect Addressing

  • (^) Direct addressing is impractical for array processing because it is not practical to use constant offsets to address more than a few array elements
  • (^) Instead, we use a register as a pointer (called indirect addressing) and manipulate the register’s value
  • (^) When an operand uses indirect addressing, it is called an indirect operand
  • (^) Indirect Operands can be implemented in
    • (^) Protected Mode
    • (^) Real Addressing Mode

Indirect Operands (1/6)

  • (^) In protected mode
    • (^) In protected mode, an indirect operand can be any 32-bit general-purpose register (EAX, EBX, ECX, EDX, ESI, EDI, EBP, and ESP) surrounded by brackets
    • (^) The register is assumed to contain the address of some data
    • (^) If the destination operand uses indirect addressing, a new value is placed in memory at the location pointed to by the register
    • (^) In the next example the contents of the BL register are copied to the memory location addressed by ESI

Indirect Operands (3/6)

  • (^) In protected mode (an array) .data val DB 10h, 20h, 30h .code MOV esi, OFFSET val MOV al, [esi] ; AL = 10h INC esi MOV bl, [esi]

Indirect Operands (4/6)

  • (^) In real address mode
    • (^) In real-address mode, a 16-bit register holds the offset of a variable
    • (^) If the register is used as an indirect operand, it may only be SI, DI, BX, or BP
    • (^) Avoid BP unless you are using it to index into the stack

Indirect Operands (6/6)

  • (^) In real address mode (an array) .data val DB 10h, 20h, 30h .code MOV si, OFFSET val MOV al, [si] INC si MOV bl, [si] ; AL = 10h ; AL = 20h

Pointer (1/3)

  • (^) A variable containing the address of another variable is called a pointer
  • (^) Pointers are a great tool for manipulating arrays and data structures, and they make dynamic memory allocation possible
  • (^) x86 programs use two basic types of pointers, NEAR and FAR Pointer Type 16 – Bit Mode 32 – Bit Mode NEAR Pointer 16-bit offset from the beginning of the data segment 32-bit offset from the beginning of the data segment FAR Pointer 32-bit segment-offset address 48-bit segment selector-offset address

Pointer (3/3)

  • (^) Pointers for arrays can be declared in the

following way

arrayB DB 10h,20h,30h,40h arrayW DW 1000h,2000h,3000h ptrB DW arrayB ptrW DW arrayW

  • (^) Optionally, we can use the OFFSET operator to

make the relationship clearer

ptrB DW OFFSET arrayB ptrW DW OFFSET arrayW

JMP Instruction

  • (^) Causes an unconditional transfer to a destination
  • (^) Destination is identified by a label which is

translated into offset at assemble time

  • (^) When CPU executed JMP, the offset of

destination is moved into the IP

dest: . . JMP dest

LOOP Instruction (2/2)

  • (^) Nested Loops
    • (^) When creating a loop inside another loop, special consideration must be given to the outer loop counter in ECX. You can save it in a variable .data count DWORD? .code mov ecx,100 ;Outer loop count L1: mov count,ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: ... ... LOOP L2 ; repeat the inner loop mov ecx,count ; restore outer loop count LOOP L1 ; repeat the outer loop

Summing an Integer Array (1/2)

  • (^) Here are the steps to calculate the sum of an array
    • (^) Assign the array’s address to a register that will serve as an indexed operand
    • (^) Initialize the loop counter to the length of the array
    • (^) Assign zero to the register that accumulates the sum
    • (^) Create a label to mark the beginning of the loop
    • (^) In the loop body, add a single array element to the sum
    • (^) Point to the next array element
    • (^) Use a LOOP instruction to repeat the loop

Copying a String

.data src DB “This is source”, 0 dst DB SIZEOF src DUP(0) .code MOV si, 0 MOV ecx, SIZEOF src L1: MOV al, src[si] MOV dst[si], al INC si LOOP L

Practice Problem

  • (^) By using the following data, implement the

expression Rval = -Xval + (Yval - Zval) in

Assembly Language

Xval DW 26 Yval DW 30 Zval DW 40