2 CPE 413 Intro To Assembly Lang Programming
2 CPE 413 Intro To Assembly Lang Programming
; Program Description:
; Author: Creation Date:
; Modified by: Modification Date:
.386
.MODEL FLAT, STDCALL
.STACK
INCLUDE\masm32\INCLUDE\windows.inc
.DATA
; (Your initialized variables here)
.DATA?
; (Your uninitialized variables here)
.CONST
;(Your constants here)
.CODE
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
• .DATA
• This section contains initialized data of your program.
• Assembler will allocate and initialize the storage of variables
• .CONST
• This section contains declaration of constants used by your program. Constants in this
section can never be modified in your program. They are just *constant*.
• CODE directive
• Defines the code section of a program containing instructions
• Assembler will place the instructions in the code area in memory
You don't have to use all .DATA, .DATA? And .CONST sections in your program.
Declare only the section(s) you want to use.
BYTE 1 byte
WORD 2
bytes
• count refers to the 16-bit value
DWORD 4 • Lo_count refers to the low byte
bytes • Hi_count refers to the high byte
QWORD 8
bytes
Prof. Christopher U. Ngene email: [email protected] 22
Where Are the Operands?
Register addressing mode
• Operands required by an • Most efficient way of
operation can be specified in a specifying an operand
variety of ways • operand is in an internal
• A few basic ways are: register
• operand in a register • Examples
• register addressing mode
• operand in the instruction itself mov EAX, EBX
• immediate addressing mode mov BX, CX
• operand in memory * The mov instruction
• variety of addressing modes
• direct and indirect addressing mov destination, source
modes
• operand
• copies data from source to
Addressing mode at an I/O
refers port
to the specification of the location of data required by an
destination
operation
Prof. Christopher U. Ngene email: [email protected] 23
Where Are the Operands?...
Immediate addressing mode Direct addressing mode
• Data is part of the instruction • Data is in the data segment
• operand is located in the code • Need a logical address to access data
• Two components: segment:offset
segment along with the
• Various addressing modes to specify
instruction the offset component
• Efficient as no separate operand • offset part is referred to as the effective
fetch is needed address
• Typically used to specify a • The offset is specified directly as part
of the instruction
constant
• We write assembly language programs
• Example using memory
Mov AL, 75 • labels (e.g., declared using DB, DW,
LABEL,...)
• This instruction uses register
• Assembler computes the offset value for
addressing mode for specifying the the label
destination and immediate • Uses symbol table to compute the offset
addressing mode to specify the of a label
source
Prof. Christopher U. Ngene email: [email protected] 24
Addressing Modes: Directing Addressing
Direct addressing mode
Examples
mov AL, response
» Assembler replaces response by its effective address (i.e., its offset value
from the symbol table)
mov table1, 56
» table1 is declared as
» Since the assembler replaces table1 by its effective address, this instruction
refers to the first element of table1
– In C, it is equivalent to
table1[0] = 56
Prof. Christopher U. Ngene email: [email protected] 25
Addressing Modes: Directing Addressing…
.code ; Equivalent to . . .
lea eax, array ; mov eax, OFFSET array
Example:
mov bl, 8Fh
movzx ax, bl
Example:
mov bl, 8Fh
movsx ax, bl
XCHG Rules
• Operands must be of the same size
• At least one operand must be a register
• No immediate operands are permitted
• No exchange of two memory operands
.DATA
arrayB BYTE 10h,20h,30h,40h
arrayW WORD 100h,200h,300h,400h
arrayD DWORD 10000h,20000h,30000h,40000h
.CODE
mov esi, 2
mov al, arrayB[esi] ; AL = 30h
mov ax, arrayW[esi*2] ; AX = 300h
mov eax, arrayD[esi*4] ; EAX = 30000h
.code
mov ebx, 2*ROWSIZE ; row index = 2
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3]
ESP
Prof. Christopher U. Ngene email: [email protected] 51
Differences between 16- and 32-bit Modes
• In assembly language,
class_marks DW 5*3 DUP (?)
• Example: Displacement of
• element is (3*3 + 1) * 2 = 20
Prof. Christopher U. Ngene email: [email protected] 58
Arrays: Example
• Example 1
• One-dimensional array
• Computes array sum (each element is 4 bytes long e.g., long
integers)
• Uses scale factor 4 to access elements of the array by using a
32-bit addressing mode (uses ESI rather than SI)
• Also illustrates the use of predefined location counter $
• Example 2
• Two-dimensional array
• Finds sum of a column
• Uses “based-indexed addressing with scale factor” to access
elements of a column
• For this reason, after initializing EBX to zero to point to the first row (line 29),
NO_ROW_BYTES is added in the loop body (line 33).
• The ESI register is used as a column index. This works for row-major ordering.
Prof. Christopher U. Ngene email: [email protected] 66
Calculating the Sizes of Arrays and Strings
• When using an array, we usually like to know its size. The following example
uses a constant named ListSize to declare the size of list:
list BYTE 10,20,30,40
ListSize = 4
• Explicitly stating an array’s size can lead to programming error, particularly if
you should later insert or remove array elements.
• A better way to declare an array size is to let the assembler calculate its value for
you
• The $ operator (current location counter) returns the offset associated with the
current program statement.
the array occupies 2 bytes (16 bit) divide addr range by 2 b/cos each word in
each element of an array of doublewords is 4 bytes long, so its overall length must be divided by four to
produce the number of array elements:
Prof. Christopher U. Ngene email: [email protected] 68