Coal Week 3 Lectures (Lec 7-9)
Coal Week 3 Lectures (Lec 7-9)
Computer Organization
& Assembly Language
INSTRUCTOR
Instructions are translated by the assembler into machine language bytes, which are
loaded and executed by the CPU at runtime.
Label (optional)
Instruction mnemonic (required)
Operand(s) (usually required)
Comment (optional)
INSTRUCTIONS
This is how the different parts are arranged:
A label is an identifier that acts as a place marker for instructions and data.
It implies the address of instruction or variable.
A data label identifies the location of a variable, providing a convenient way to
reference the variable in code.
Example
count DWORD 100
array DWORD 1024, 2048
INSTRUCTIONS
A label in the code area of a program (where instructions are located) must end with
a colon (:) character.
. Code labels are used as targets of jumping and looping instructions.
Example
A code label can share the same line with an instruction, or it can be on a line by itself:
Instruction Mnemonic
An instruction mnemonic is a short word that identifies an instruction.
It provide hints about the type of operation they perform.
Operands
An operand is a value that is used for input or output for an instruction.
Assembly language instructions can have between zero and three operands.
Each of which can be a register, memory operand, integer expression, or input–output
port.
Instruction Format Examples
No operands
– stc ; set Carry flag
One operand
– inc eax ; register
– inc myByte ; memory
Two operands
– add ebx,ecx ; register, register
– sub myByte,25 ; memory, constant
– add eax,36 * 25 ; register, constant-expression
Instruction Format Examples
Three operands
- imul eax, ebx, 5
x86 processors are designed to load code and data more quickly from even doubleword
addresses.
Integer Constants
An integer literal (also known as an integer constant ) is made up of an optional leading
sign, one or more digits, and an optional radix character that indicates the number’s
base:
A hexadecimal literal beginning with a letter must have a leading zero to prevent the
assembler from interpreting it as an identifier.
Such is the case with the hexadecimal value A3h in the foregoing list, which must be
written as 0A3h.
CLASS ACTIVITY
Precedence refers to the implied order of operations when an expression contains two or more
operators
Integer Expressions
Example:
DEFINING DATA
The assembler recognizes a basic set of intrinsic data types, which describe types in
terms of their size.
[name] directive initializer [, initializer]...
Initializer: At least one initializer is required in a data definition, even if it is zero.
.data
sum DWORD 0
If you prefer to leave the variable uninitialized (assigned a random value), the ? symbol
can be used as the initializer
sum DWORD ?
DEFINING DATA
DEFINING DATA
Defining BYTE and SBYTE Data
• Each initializer must fit into 8 bits of storage
value1 BYTE 'A’ ; character constant
value2 BYTE 0 ; smallest unsigned byte
value3 BYTE 255 ; largest unsigned byte
value4 SBYTE -128 ; smallest signed byte
value5 SBYTE +127 ; largest signed byte
value6 BYTE ? ; uninitialized byte
DEFINING DATA
Multiple Initializers
•If multiple initializers are used in the same data definition, its label refers only to the offset of
the first initializer.
•The rule that byte values must be separated by commas does not apply on strings.
DUP OPERATOR
The DUP operator allocates storage for multiple data items, using a integer expression as a
counter.
Define
i. Largest unsigned Value (32-bits)
ii. Smallest Signed Value (32-bits)
iii. Unsigned Array. (32- bits)
iv. Signed Array . (32-bits)
Real Number
Real number constants are represented as decimal reals or encoded (hexadecimal) reals.
A decimal real contains are optional sign followed by an integer, a decimal point an optional
integer that expresses a fraction, and an optional exponent:
[sign] integer. [integer] [exponent]
Following are the syntax for the sign and exponent:
sign {+,-}
exponent E [ { + , - } ] integer
Following are examples of valid number constants:
2.
+3.0
-44.2E+05
26.E5
At least one digit and a decimal point are required.
Real Number
• .data
• .code
• .stack
• DWORD
DIRECTIVE VS INSTRUCTION
myVar DWORD 26
DWORD directive tells the assembler to reserve space in
the program for a doubleword variable.
mov eax,myVar
The MOV instruction, on the other hand, executes at
runtime, copying the contents of myVar to the EAX
register.
Class Room Activity
Given the number 456789ABh, list out its byte values in
little endian order.
Write a program that calculates the following
expression, using registers:
A = (A + B) − (C + D).
Assign integer values to the EAX, EBX, ECX, and EDX
registers.
Class Room Activity
(True/False): A code label is followed by a colon (:), but
a data label does not end with a colon.
Which data directive creates a 32-bit signed integer
variable?
Show the order of individual bytes in memory (lowest to
highest) for the following double-word variable:
EXAMPLE CODE
EXAMPLE CODE (EXPLANATION)
.386 directive identifies the program as a 32-bit program that can access 32-bit
registers and addresses.
.model flat, stdcall selects the programs memory model, and identifies the calling
convention.
The stdcall keyword tells the assembler how to manage the runtime stack when
procedures are called.
It tells the assembler how many bytes of memory to reserve for the
program’s runtime stack.
Stack are used :
to hold passed parameters
to hold the address of the code that called the function. The CPU uses
this address to return when the function call finishes, back to the spot
where the function was called.
Stack can hold local variables.
EXAMPLE CODE (EXPLANATION)
When your program is ready to finish, it calls ExitProcess that returns an integer that
tells the operating system that your program worked just fine.
Line 16 uses the END directive to mark the last line to be assembled (end of program),
and it identifies the program entry point (main).
If you add any more lines to a program after the END directive, they will be ignored by
the assembler.
REVIEW QUESTIONS
What is the Purpose of TITLE Directive?
It marks entire line as comment.
Example → TITLE Add and Subtract (AddSub.asm)