2 CPE 413 Intro To Assembly Lang Programming
2 CPE 413 Intro To Assembly Lang Programming
Language
; 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.
Prof. Christopher U. Ngene email: [email protected] 11
INCLUDE, PROC, ENDP, and END
• INCLUDE directive
• Causes the assembler to include code from another file
• We will include windows.inc
• Declares procedures implemented in the windows.lib library
• To use this library, you should link windows.lib to your programs
• PROC and ENDP directives
• Used to define procedures
• As a convention, we will define main as the first procedure
• Additional procedures can be defined after main
• END directive
• Marks the end of a program
• Identifies the name (main) of the program’s startup procedure
Correspondence to C Data
Types
BYTE 1 byte
WORD 2 bytes
DWORD 4 bytes
• count refers to the 16-bit
QWORD 8 bytes
value
TWORD 10 bytes • Lo_count refers to the low
byte
• Hi_count refers to the high
Prof. Christopher U. Ngene email: [email protected] 22
byte
Where Are the Operands?
Register addressing
• Operands required by an mode
• Most efficient way of
operation can be specified in specifying an operand
a variety of ways • operand is in an internal
• A few basic ways are: register
• operand in a register
• register addressing mode
• Examples
• operand in the instruction itself mov EAX, EBX
• immediate addressing mode mov BX, CX
• operand in memory
• variety of addressing modes
* The mov instruction
• direct and indirect addressing mov destination, source
modes
• operand at an I/O port • copies data from source
Addressing mode refers to the specificationto destination
of the location of data required by
an operation
Prof. Christopher U. Ngene email: [email protected] 23
Where Are the Operands?...
Immediate addressing mode Direct addressing mode
mov table1, 56
» table1 is declared as
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
.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]
• 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.
• 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.
• In the following example, ListSize is calculated by subtracting the offset of list from
the current location counter ($):
list BYTE 10,20,30,40
ListSize = ($ - list)
ListSize must follow immediately after lis
Prof. Christopher U. Ngene email: [email protected] 67
Calculating the Sizes of Arrays and Strings…
• Rather than calculating the length of a string manually, let the assembler do it:
myString BYTE "This is a long string, containing"
BYTE "any number of characters"
myString_len = ($ − myString)
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: