Week 3 - Lecture
Week 3 - Lecture
Malware Forensics
x86 Assembly I (Review)
Instruction Set Architecture (ISA)
● Is an abstract model of a computer, which defines the supported instructions,
data types, registers, etc.
● ISA specifies the behavior of machine code running on implementations of
that ISA
● Examples: Intel x86-64, ARM variants, MIPS, etc
○ https://www.mips.com/products/architectures/mips32-2/
Micro-architecture
● Micro-architecture is the way a given instruction set architecture (ISA) is
implemented in hardware (i.e., microchips) – i.e., the processor.
Comments
Program Structure in Assembly* (Review)
Function return values are typically stored in the EAX register for integer and pointer values. Therefore to use returned
value you must access it in EAX (See last lab question).
Example Windows Library call – Displaying a Message Box (Def. below)
Example Windows Library call – Displaying a Message Box (Code and Running below)
Example C Library call (MSVCRT.dll) – printf() (Code and Running below)
Assembly II
x86 Instructions (Reference)
mov <reg>,<mem>
mov <mem>,<reg>
mov <reg>,<const>
mov <mem>,<const>
e.g., mov eax, ebx — copy the value in ebx into eax
Add / Sub - Add / Subtract
add <reg>,<reg> sub <reg>,<reg>
inc <reg>
inc <mem>
dec <reg>
dec <mem>
imul <reg32>,<reg32>
imul <reg32>,<mem>
imul <reg32>,<reg32>,<con>
imul <reg32>,<mem>,<con>
imul eax, [var] — multiply the contents of EAX by the 32-bit contents of the memory
location var. Store the result in EAX.
These instructions perform the specified logical operation (logical bitwise and, or, and
exclusive or, respectively) on their operands, placing the result in the first operand location.
and eax, 0fH — clear all but the last 4 bits of EAX.
xor edx, edx — set the contents of EDX to zero (clear all).
not — Bitwise Logical Not
Logically negates the operand contents (that is, flips all bit values in the operand).
not <reg>
not <mem>
not BYTE PTR [var] — negate all bits in the byte at the memory location var.
neg - Negate
neg <reg>
neg <mem>
Example
These instructions shift the bits in their first operand's contents left and right, padding the resulting empty bit positions with zeros. The
shifted operand can be shifted up to 31 places. The number of bits to shift is specified by the second operand, which can be either an 8-
bit constant or the register CL. In either case, shifts counts of greater then 31 are performed modulo 32.
shl <reg>,<con8>
shl <mem>,<con8>
shl <reg>,<cl>
shl <mem>,<cl>
shr <reg>,<con8>
shr <mem>,<con8>
shr <reg>,<cl>
shr <mem>,<cl>
shl eax, 1 — Multiply the value of EAX by 2 (if the most significant bit is 0)
shr ebx, cl — Store in EBX the floor of result of dividing the value of EBX by 2n wheren is the value in CL.
jmp - Jump
Transfers program control flow to the instruction at the memory location indicated by the operand.
jmp <label>
Compare the values of the two specified operands, setting the condition codes in the machine
status word appropriately. This instruction is equivalent to the sub instruction - the result of the
subtraction is discarded, though.
cmp <reg>,<reg>
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>
Status Register, Memory addressing,
Conditional statements and
Looping in Assembly
General Purpose Register Sizes
Values Range
Assembly Sizes
1 Bit = 0/1
Qword = 8 bytes
x86 CPU Registers and Status Reg.
+ EFLAGS
EGLAGS is the “flags register”, a 32-bit status register, that records the outcome (status) of
operations
Flags Register: EFLAGS
● EFLAGS register is a status register. 32 bits in size, and each bit is a flag.
● During execution, each flag is either set (1) or cleared (0) to control CPU operations or
indicate the results of a CPU operation.
● CF The carry flag is set when the result of an operation is too large or too small for the
destination operand; otherwise, it is cleared.
● SF The sign flag is set when the result of an operation is negative or cleared when the result is
positive. This flag is also set when the most significant bit is set after an arithmetic operation.
● TF The trap flag is used for debugging. The x86 processor will execute only one instruction at a
time if this flag is set.
Flags Register: EFLAGS
Memory addressing
… 21 1F 00 00 … RAM
Hex constant
too_young:
<print msg and exit> Cannot vote.
Example Modifying Letters
Loops
Loops via de-incrementing
● Define a loop counter (e.g., ebx) and initialise it
● Define loop label
● Add loop’s functionality
● De-increment inside of the loop
● Compare to 0 and jump if not zero (not equal)
● Loop until it is zero