Moving Smaller Data Types to Larger Ones | |
---|---|
Instruction | Description |
movsz eax, bx |
Use this for unsigned values(only positive).Assigns the value in bx, to eax by adding all zeros in the additional 2 byte space ahead of bx. The Z is for zero. A way to replicate this is by moving 0 to the larger destination register first, then moving the value to the smaller version of the destination register. |
movsx eax, bx |
Use this for signed values(positive or negative). Assigns the value in bx, to eax. |
"div" Examples: Unsigned Division Operator | |
---|---|
Instruction | Description |
mov ax, 13 mov bl, 3 div bl |
8 bit Unsigned Division. Divides 13 by 3. The result will be stored in AL (4), the remainder will be stored in AH (1). Note: AX has to be assigned, you cannot use a different register for the dividend. |
mov dx, 0 mov ax, 13 mov cx, 3 div cx |
16 bit Unsigned Division. Divides 13 by 3. The result will be stored in AX (4), the remainder will be stored in DX (1). Note: DX and AX have to be assigned, you cannot use different registers for the dividend. |
mov edx, 0 mov eax, 13 mov ecx, 3 div ecx |
32 bit Unsigned Division. Divides 13 by 3. The result will be stored in EAX (4), the remainder will be stored in EDX (1). Note: EDX and EAX have to be assigned, you cannot use different registers for the dividend. |
Signed Extension Instructions | |
---|---|
Instruction | Description |
.data byteVal SBYTE -101 ; 9Bh .code mov al, byteVal ; AL = 9Bh cbw ; AX = FF9Bh |
CBW means convert byte to word. CBW converts signed value in AL, and stores the result in AX |
.data wordVal SWORD -101 ; FF9Bh .code mov ax, wordVal ; AX = FF9Bh cwd ; DX = FFFFh ; AX = FF9Bh |
CWD means convert word to double word. CWD converts signed value in AX, and stores the result in DX and AX |
.data dwordVal SDWORD -101 ; FFFFFF9Bh .code mov eax, dwordVal cdq ; EDX = FFFFFFFFh ; EAX = FFFFFF9Bh |
CDQ means convert double word to quad word. CDQ converts signed value in EAX, and stores the result in EDX and EAX |
"idiv" Examples: Signed Division Operator | |
---|---|
Instruction | Description |
mov ax, 13 mov bl, 3 idiv bl |
8 bit Signed Division. Divides 13 by 3. The result will be stored in AL (4), the remainder will be stored in AH (1). Note: AX has to be assigned, you cannot use a different register for the dividend. |
mov dx, 0 mov ax, 13 mov cx, 3 idiv cx |
16 bit Signed Division. Divides 13 by 3. The result will be stored in AX (4), the remainder will be stored in DX (1). Note: DX and AX have to be assigned, you cannot use different registers for the dividend. |
mov edx, 0 mov eax, 13 mov ecx, 3 idiv ecx |
32 bit Signed Division. Divides 13 by 3. The result will be stored in EAX (4), the remainder will be stored in EDX (1). Note: EDX and EAX have to be assigned, you cannot use different registers for the dividend. |
Division Operator Notes | |
---|---|
Problem | Solution |
If the result wont fit inside the destination operand, the program will crash. |
Store the operands in larger registers (if possible). |
Dividing by zero |
Compare (cmp) the divisor against zero and jump if equal to (je) past the division to prevent program crashing. |
Unconditional Jump | |
---|---|
Instruction | Description |
mov eax, 0 L1: inc eax jmp L1 |
Jumps to L1 infinitely. |
Compare Operator | |
---|---|
Instruction | Description |
mov eax, 0 L1: inc eax cmp eax, 10 jb L1 |
Jumps to L1 while eax is less than (below) 10. |
Conditional Jumps (After Signed Comparison) | |
---|---|
Instruction | Description |
jg |
Jump if Greater. Signed Flag Equals Overflow Flag and Zero Flag Equals 0. |
jnle |
Jump if NOT Less Than or Equal to. |
jge |
Jump if Greater or Equal to. Signed Flag Equals Overflow Flag |
jnl |
Jump if NOT Less Than. |
jl |
Jump if Less Than. Signed Flag Does Not Equal Overflow Flag. |
jnge |
Jump if NOT Greater Than or Equal to. |
jle |
Jump if Less Than or Equal to. Signed Flag Does Not Equal Overflow Flag and Zero Flag Equals 1. |
jng |
Jump if NOT Greater Than. |
Conditional Jumps (After Unsigned Comparison) | |
---|---|
Instruction | Description |
ja |
Jump if Above. Carry Flag Equals 0 and Zero Flag Equals 0. |
jnbe |
Jump if NOT Below or Equal. |
jae |
Jump if Above or Equal. |
jnb |
Jump if NOT Below. |
jb |
Jump if Below. Carry Flag Equals 0. |
jnae |
Jump if NOT Above or Equal. |
jbe |
Jump if Below or Equal. |
jna |
Jump if NOT Above. |
Conditional Jumps (Additional) | |
---|---|
Instruction | Description |
je |
Jump if Equal. Zero Flag Equals 1. |
jz |
Jump if Zero. |
jne |
Jump if NOT Equal. Zero Flag Equals 0. |
jnz |
Jump if NOT Zero. |
js |
Jump if Sign. Signed Flag Equals 1. |
jc |
Jump if Carry. Carry Flag Equals 1. |
jo |
Jump if Overflow. Overflow Equals 1. |
jeaxz |
Jump if value in EAX is Zero. |
Note: |
Many Others Exist. Ex. jns, jnc, jno |
Loops | |
---|---|
Instruction | Description |
mov ecx, 0 L1: cmp ecx, 10 je EndL1 ; Other Code Goes Here inc ecx jmp L1 EndL1: |
While Loop. Always Jump at the End, and Compare at the begining. |
mov ecx, 10 L1: ; Other Code Here loop L1 |
Loop instruction. This will jump to L1 until ecx is equal to zero. This will auto decrement ecx each time it is called. |
mov ecx, 10 L1: ; Other Code Here loop L1 |
For Loop. Use the loop Operator. ECX stores the current iteration (from upper bound to lower). See Loop Instruction For Details. |
mov eax, 0 L1: ; Other Code Goes Here inc eax cmp eax, 10 jb L1 |
(Do while) Until Loop. Dont Compare for Jump Till the End. |
Note: |
ecx is used in loops by several operators. jecxz can be used to prevent infinite loops if ecx were to reach 0. |
Arrays | |
---|---|
Instruction | Description |
arrayName DWORD 1, 1, 2, 3, 5, 8, 13, 21 |
Array Definition. Length is 8. Values are Fibonacci numbers. |
arrayName DWORD 9 DUP (?) |
Array Definition. Length is 9. Values are essentially null/0 |
arrayName DWORD 10 DUP (1) |
Array Definition. Length is 10. Values are all 1 |
arrayName DWORD 1, 2, 3, 4, 5 mov ecx, 3 mov edx, arrayName[4*ecx] ; Gets 4 bytes at index 3. |
The array is Length 5. Gets 4 bytes(the size of edx) at index 3(12 bytes skipped). EDX Will Contain 4 |
arrayName WORD 1, 2, 3, 4, 5 mov cx, 4 mov dx, arrayName[2*cx] ; Gets 4 bytes at index 4. |
The array is Length 5. Gets 2 bytes(the size of dx) at index 4(8 bytes skipped). DX Will Contain 5 |
Basic Stack Operations | |
---|---|
Instruction | Description |
push eax |
Pushes value of EAX onto the stack. (Works with WORD and DWORD) |
pushd eax |
Pushes value of EAX onto the stack. (Works with DWORD) |
pushw ax |
Pushes value of AX onto the stack. (Works with WORD) |
Notes For Pushing: |
Each Push Decrements ESP by the size of the object pushed. The Object is inserted at ESP after ESP has been moved. Flags are not changed. |
pop eax |
Pops the Value off of the stack and stores it in EAX. (Works with WORD and DWORD) |
Notes For Popping: |
The Object is copied from stack at ESP to the destination (Size determined by the input size from the related push). Then Pop Increments ESP by the size of the object. Flags are not changed. |
Notes For Stack: |
Order Matters!!!!!!!!!! See Examples Below. |
push eax push edx push edi ; Other Code Goes Here pop edi pop edx pop eax |
Example 1: Order is maintained. EAX, EDX, and EDI Values Are Protected/Restored. |
pushad pushfd ; Other Code Goes Here popfd popad |
Example 2: Order is maintained. All Registers and Flags Are Protected/Restored. |
|
Empty Stack. Of Size 8 |
push 1h ; One Byte push Fh ; One Byte mov eax, FFEEDDCCh push eax ; Four Bytes
|
Stack after three pushes. Of Size 8. The compiler should throw errors if you attempt to pop off the wrong size variable. |
push 1h ; One Byte push Fh ; One Byte mov eax, FFEEDDCCh push eax ; Four Bytes mov dx 110h push dx ; Two Bytes
|
Stack after four pushes. Of Size 8. The compiler should throw errors if you attempt to pop off the wrong size variable. |
Additional Stack Operations | |
---|---|
Instruction | Description |
pushad |
Pushes value of All Registers onto the stack. |
pushfd |
Pushes value of EFlagS Register onto the stack. |
popad |
Pops the Values off of the stack and stores the associated registers. |
popfd |
Pops the Value off of the stack and stores the associated registers. |
Addressing the Stack | |
---|---|
Instruction | Description |
mov eax, [ebp + 8] |
Moves the value of the first (latest added) variable into eax. |
mov eax, [ebp + 12] |
Moves the value of the second variable into eax. |
mov ax, [ebp + 10] |
Moves the value of the second variable into ax. Assuming the first and second Values are WORD. |
push ax push ebx push ecx push dx [ebp+8] ; dx [ebp+10] ; ecx [ebp+14] ; ebx [ebp+18] ; ax |
Number on the right is calculated with, 8+(Sum of each previous variable's size). |
Procedure Operations | |
---|---|
Sub-Program. Self Contained. Similar to a Function. |
|
Instruction | Description |
.CODE procName PROC ; Other Code Goes Here procName ENDP |
Definition of a Procedure |
call procName |
Calling a Previously Defined Procedure |
ret |
Similar to Return. This basically sets the instruction pointers back to previous program. |
Note: |
Push and Pop operations are generally self contained. Do NOT attempt to push values onto the stack to return them. So if you enter the proc with 3 DWORDs in stack, you must exit with at most 3 DWORDs. |
ret n |
Similar to ret. This will pop n bytes off of the stack to ensure stack returning to previous state. This basically sets the instruction pointers back to previous program. |
Associated Definitions: |
Parameters are in the Definition of the Procedure. Arguments are sent during a Procedure Call. |
Note: |
Parameters are passed on the stack prior the Procedure Call. |
.CODE procName PROC push ebp ; Protect Stack Frame mov ebp, esp ; Protects Local Variables sub esp, 0 ; Replace 0 with the number of bytes for localVars ; Other Register Pushes Go Here pushfd ; Other Code Goes Here popfd ; Other Register Pops Go Here pop ebp ; Restore Stack Frame ret procName ENDP |
Full Example of a Procedure Definition. |
Note: |
Stack changes during procedure. I recommend looking at Lecture 20 Slide 15 and 16 for Examples. |
EXTERN procName:PROC |
Expose the PROC for other asm files to use. |
Pointer/Address Operations | |
---|---|
Instruction | Description |
lea eax, variableName |
Moves the Memory Address of variableName into eax |
mov eax, OFFSET variableName |
This is 1 CPU Cycle Faster! Moves the Memory Address of variableName into eax |
mov [eax], 10 |
Moves the value 10 into the variable whose address is in eax. |
lea eax, variableName mov [eax], 10 |
Moves the value 10 into variableName. |
lea eax, variableName mov ebx, [eax] |
Moves the value of variableName into ebx. |
Local Variable Operations | |
---|---|
Instruction | Description |
Notes: |
Starts at [ebp-4], then decrement by the size of the required variable. This is the opposite of the push/pop allocations. Make Sure to move esi past the end of the lowest LocalVariable to prevent overwriting them. |
.CODE procName PROC push ebp ; Protect Stack Frame mov ebp, esp ; Protects Local Variables sub esp, 8 ; Replace 0 with the number of bytes for localVars ; local var 1 is [ebp-4] size of 4 bytes ; Local Var 2 is [ebp-8] size of 4 bytes ; Other Register Pushes Go Here pushfd ; Other Code Goes Here popfd ; Other Register Pops Go Here pop ebp ; Restore Stack Frame ret procName ENDP |
Example 1: ESP is decremented by 8, allowing 8 bytes to be safe. |
Macros | |
---|---|
Instruction | Description |
nameOfMacro MACRO arg1, arg2, arg3 ; Other Code Goes Here ENDM |
Definition of A Macro with 3 args (you can put as many as you want) |
Notes: |
Basically A Function. This is pass by reference. |