└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # ASSEMBLY 2 | **! KEEP OUT !** 3 | or enter 4 | I am a sign, not a cop 5 | 6 | ## PROCESSOR REGISTERS 7 | ### GENERAL REGISTERS 8 | #### DATA REGISTERS 9 | **32-bit:** EAX, EBX, ECX, EDX 10 | **16-bit:** [A-D]X 11 | **8-bit:** [A-D]L, [A-D]H 12 | 13 | - *AX - primary accumulator* - arithmetic instructions 14 | - *BX - base register* - indexed addressing 15 | - *CX - count register* - stores the loop count in iterative operations 16 | - *DX - data register* - input/output 17 | 18 | #### POINTER REGISTERS 19 | - *IP - Instruction Pointer* - stores the offset address of the next instruction 20 | - *SP - Stack Pointer* - provides the offset value within the program stack 21 | - *BP - Base Pointer* - references the parameter variables passed to a subroutine 22 | 23 | #### INDEX REGISTERS 24 | - *SI - Source Index* - source index for string operations 25 | - *DI - Destination Index* - destionation index for string operations 26 | 27 | ### CONTROL REGISTERS 28 | The 32-bit instruction pointer register and the 32-bit flags register combined are considered as the control registers. 29 | 30 | - *OF - Overflow Flag* − indicates the overflow of a high-order bit (leftmost bit) of data after a signed arithmetic operation. 31 | - *DF - Direction Flag* − determines direction for moving or comparing string. df = 0 L->R, df = 1 R->L 32 | - *IF - Interrupt Flag* − determines whether the external interrupts like keyboard entry, are to be ignored or processed. if = 0 disable, if = 1 enable 33 | - *TF - Trap Flag* − allows setting the operation of the processor in single-step mode, so we could step through the execution one instruction at a time. 34 | - *SF - Sign Flag* − shows the sign of the result of an arithmetic operation. positive -> sf = 0, negative -> sf = 1 35 | - *ZF - Zero Flag* − indicates the result of an arithmetic or comparison operation. nonzero -> zf = 0, zero -> zf = 1. 36 | - *AF - Auxiliary Carry Flag* − is set when a 1-byte arithmetic operation causes a carry from bit 3 into bit 4. 37 | - *PF - Parity Flag* − indicates the total number of 1-bits in the result obtained from an arithmetic operation. even -> pf = 0, odd -> pf = 1 38 | - *CF - Carry Flag* − contains the carry of 0 or 1 from a high-order bit after an arithmetic operation, stores the contents of last bit of a shift or rotate operation. 39 | 40 | ### SEGMENT REGISTERS 41 | - *Code Segment* - contains all the instructions to be executed. 16-bit CS register stores the starting address of the code segment. 42 | - *Data Segment* − contains data, constants and work areas. 16-bit DS register stores the starting address of the data segment. 43 | - *Stack Segment* − contains data and return addresses of procedures or subroutines. 16-bit SS register stores the starting address of the stack. 44 | 45 | **!** ES, FS and GS provide additional segments for storing data. 46 | 47 | ## SYSTEM CALLS 48 | - 1 -> sys_exit 49 | - 2 -> sys_fork 50 | - 3 -> sys_read 51 | - 4 -> sys_write 52 | - 5 -> sys_open 53 | - 6 -> sys_close 54 | 55 | system call number -> eax 56 | arguments -> ebx, ecx, edx, esi, edi, ebp 57 | interrupt -> int 58 | 59 | ``` 60 | mov edx,4 ; message length 61 | mov ecx,msg ; message to write 62 | mov ebx,1 ; file descriptor (stdout) 63 | mov eax,4 ; system call number (sys_write) 64 | int 0x80 ; call kernel 65 | ``` 66 | 67 | ## DIRECTIVES 68 | 69 | ### DEFINE 70 | Used for storage allocation statement for initialized data. 71 | - DB - Define Byte - 8 bit 72 | - DW - Define Word - 16 bit 73 | - DD - Define DoubleWord - 32 bit 74 | 75 | `msg db 'Hello, world!'` 76 | 77 | ### RESERVE 78 | Used for reserving space for uninitialized data. 79 | - RESB - Reserve Byte 80 | - RESW - Reserve Word 81 | - RESD - Reserve Doubleword 82 | 83 | `msg resb 64` 84 | 85 | ### TIMES 86 | Allows multiple initializations to the same value. Useful in defining arrays and tables. 87 | 88 | `stars times 7 db '*' ;*******` 89 | 90 | ### EQU 91 | Defines constants. 92 | 93 | `TOTAL equ 73` 94 | 95 | ### %assign 96 | Defines numeric constants like. Allows redefinition, case-sensitive. 97 | 98 | `%assign TOTAL 10` 99 | 100 | ### %define 101 | Defines both numeric and string constants. Allows redefinition, case-sensitive. 102 | 103 | `%define TOTAL 43` 104 | 105 | ## INSTRUCTIONS 106 | 107 | ### DATA TRANSFER INSTRUCTIONS 108 | #### MOV 109 | Used for moving data from one storage space to another. The value of source operand remains unchanged. 110 | 111 | `MOV destination, source` 112 | 113 | - DEST, SOURCE 114 | - register, register 115 | - register, immediate 116 | - memory, immediate 117 | - register, memory 118 | - memory, register 119 | 120 | !! Both the operands in MOV operation should be of same size 121 | 122 | #### MOVSX 123 | Move with sign extension. Copies the src operand in the dest operand and pads the remaining bits not provided by src with the sign bit (the MSB) of src. 124 | 125 | `MOVSX EAX, AL` 126 | 127 | #### MOVZX 128 | Move with zero extension. Copies data from the src operand to the dest operand, but the the remaining bits in dest that are not provided by src are filled with zeros. Useful for copying a small, unsigned value to a bigger register. 129 | 130 | `MOVZX EAX, AL` 131 | 132 | #### XCHG 133 | Swaps the src operand with the dest operand. 134 | 135 | `XCHG AX, BX` 136 | 137 | #### LEA 138 | Load effective address. Calculates the address of the src operand and loads it into the dest operand. 139 | 140 | `LEA SI, data` 141 | 142 | ### ARITHMETIC INSTRUCTIONS 143 | #### INC 144 | Increments an operand (8-bit, 16-bit or 32-bit) by one. 145 | ``` 146 | INC EBX ; Increments 32-bit register 147 | ``` 148 | 149 | #### DEC 150 | Decrements an operand (8-bit, 16-bit or 32-bit) by one. 151 | ``` 152 | DEC DL ; Decrements 8-bit register 153 | DEC [count] ; Decrements the count variable 154 | ``` 155 | 156 | #### ADD and SUB 157 | Used for performing simple addition/subtraction of binary data in byte, word and doubleword size. 158 | 159 | `ADD/SUB destination, source ` 160 | 161 | - DEST, SOURCE 162 | - Register to register 163 | - Memory to register 164 | - Register to memory 165 | - Register to constant data 166 | - Memory to constant data 167 | 168 | #### MUL and IMUL 169 | Both are used for multiplying binary data. The MUL (Multiply) for unsigned data and the IMUL (Integer Multiply) for signed data. 170 | 171 | `MUL/IMUL multiplier` 172 | 173 | - *For two bytes:* The multiplicand is in the AL register, and the multiplier is a byte in the memory or in another register. The product is in AX. High-order 8 bits of the product is stored in AH and the low-order 8 bits are stored in AL 174 | 175 | - *For two one-word values:* The multiplicand should be in the AX register and the multiplier is a word. For example, for an instruction like MUL DX, you must store the multiplier in DX and the multiplicand in AX. The resultant product is a doubleword, which will need two registers. The high-order (leftmost) portion gets stored in DX and the lower-order (rightmost) portion gets stored in AX. 176 | 177 | - *For two doubleword values:* The multiplicand should be in EAX and the multiplier is a doubleword value. The product generated is stored in the EDX:EAX registers, i.e., the high order 32 bits gets stored in the EDX register and the low order 32-bits are stored in the EAX register. 178 | 179 | #### DIV and IDIV 180 | Generates two elements - a quotient and a remainder. The DIV (Divide) for unsigned data and the IDIV (Integer Divide) for signed data. 181 | 182 | !! In case of division, overflow may occur. The processor generates an interrupt if overflow occurs. 183 | 184 | `DIV/IDIV divisor` 185 | 186 | - *If divisor is byte:* The dividend is assumed to be in the AX register (16 bits). After division, the quotient goes to the AL register and the remainder goes to the AH register. 187 | 188 | - *If divisor is word:* The dividend is assumed to be 32 bits long and in the DX:AX registers. The high-order 16 bits are in DX and the low-order 16 bits are in AX. After division, the 16-bit quotient goes to the AX register and the 16-bit remainder goes to the DX register. 189 | 190 | - *If divisor is doubleword:* The dividend is assumed to be 64 bits long and in the EDX:EAX registers. The high-order 32 bits are in EDX and the low-order 32 bits are in EAX. After division, the 32-bit quotient goes to the EAX register and the 32-bit remainder goes to the EDX register. 191 | 192 | ### LOGICAL INSTRUCTIONS 193 | #### AND 194 | It returns 1, if the matching bits from both the operands are 1, otherwise it returns 0. 195 | 196 | `AND AL, 01H ; ANDing with 0000 0001` 197 | 198 | #### OR 199 | It returns 0, if both the bits are zero, otherwise it returns 1. 200 | 201 | `OR AL, BL` 202 | 203 | #### XOR 204 | If the bits from the operands are same (both 0 or both 1), the resultant bit is cleared to 0, otherwise it sets to 1. 205 | 206 | `XOR EAX, EAX` 207 | 208 | #### TEST 209 | Works same as the AND operation, but it does not change the first operand. It can be used to check whether a number in a register is even or odd. 210 | 211 | `TEST AL, 01H` 212 | 213 | #### NOT 214 | Reverses the bits in an operand. The operand could be either in a register or in the memory. 215 | 216 | `NOT eax` 217 | 218 | ### CALLING INSTRUCTIONS 219 | #### CALL 220 | Pushes the return address (address immediately after the CALL instruction) on the stack. Changes EIP to the call destination. This effectively transfers control to the call target and begins execution there. 221 | 222 | `CALL function_name` 223 | 224 | #### INT 225 | Generates a software interrupt. 226 | 227 | `INT 0x80` 228 | 229 | #### RET 230 | Transfers program control to a return address located on the top of the stack. This address is usually placed on the stack by a CALL instruction. 231 | 232 | `RET` 233 | 234 | ## SECTIONS 235 | - *section.data:* 236 | constant values, file names, buffer size etc. does not change at runtime 237 | 238 | - *section.bss* 239 | variables 240 | 241 | - *section.text* 242 | actual code 243 | 244 | ``` 245 | section .text 246 | global _start ;must be declared for linker (ld) 247 | 248 | _start: ;tells linker entry point 249 | mov edx,len ;message length 250 | mov ecx,msg ;message to write 251 | mov ebx,1 ;file descriptor (stdout) 252 | mov eax,4 ;system call number (sys_write) 253 | int 0x80 ;call kernel 254 | 255 | mov eax,1 ;system call number (sys_exit) 256 | int 0x80 ;call kernel 257 | 258 | section .data 259 | msg db 'Hello, world!', 0xa ;string to be printed 260 | len equ $-msg ;length of the string 261 | ``` 262 | 263 | ## CONDITIONS 264 | ### CMP Instruction 265 | Compares two operands. It is generally used in conditional execution. Subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. 266 | 267 | `CMP destination, source` 268 | 269 | ### UNCONDITIONAL JUMP 270 | Provides a label name where the flow of control is transferred immediately. 271 | 272 | `JMP label` 273 | 274 | ``` 275 | L20: 276 | ADD AX, 01 ; Increment AX 277 | ADD BX, AX ; Add AX to BX 278 | JMP L20 ; repeats the statements 279 | ``` 280 | 281 | ### CONDITIONAL JUMP 282 | This is performed by a set of jump instructions j-condition- depending upon the condition. The conditional instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in IP. If some specified condition is satisfied, the control flow is transferred to a target instruction. 283 | 284 | *For signed data:* 285 | 286 | - INSTRUCTION --- DESCRIPTION --- FLAGS 287 | - JE / JZ --- Jump Equal or Jump Zero --- ZF 288 | - JNE / JNZ --- Jump not Equal or Jump Not Zero --- ZF 289 | - JG / JNLE --- Jump Greater or Jump Not Less/Equal --- OF,SF,ZF 290 | - JGE / JNL --- Jump Greater/Equal or Jump Not Less --- OF,SF 291 | - JL / JNGE --- Jump Less or Jump Not Greater/Equal --- OF,SF 292 | - JLE / JNG --- Jump Less/Equal or Jump Not Greater --- OF,SF,ZF 293 | 294 | *For unsigned data:* 295 | - INSTRUCTION --- DESCRIPTION --- FLAGS 296 | - JE/JZ --- Jump Equal or Jump Zero --- ZF 297 | - JNE/JNZ --- Jump not Equal or Jump Not Zero --- ZF 298 | - JA/JNBE --- Jump Above or Jump Not Below/Equal --- CF-ZF 299 | - JAE/JNB --- Jump Above/Equal or Jump Not Below --- CF 300 | - JB/JNAE --- Jump Below or Jump Not Above/Equal --- CF 301 | - JBE/JNA --- Jump Below/Equal or Jump Not Above --- AF-CF 302 | 303 | *For check the value of flags:* 304 | - INSTRUCTION --- DESCRIPTION --- FLAGS 305 | - JXCZ --- Jump if CX is Zero --- none 306 | - JC --- Jump If Carry --- CF 307 | - JNC --- Jump If No Carry --- CF 308 | - JO --- Jump If Overflow --- OF 309 | - JNO --- Jump If No Overflow --- OF 310 | - JP/JPE --- Jump Parity or Jump Parity Even --- PF 311 | - JNP/JPO --- Jump No Parity or Jump Parity Odd --- PF 312 | - JS --- Jump Sign (negative value) --- SF 313 | - JNS --- Jump No Sign (positive value) --- SF 314 | 315 | ``` 316 | CMP AL, BL 317 | JE IS_EQUAL 318 | CMP AL, BH 319 | JE NON_EQUAL 320 | NON_EQUAL: ... 321 | IS_EQUAL: ... 322 | ``` 323 | 324 | ## LOOP 325 | Assumes that the ECX register contains the loop count. When it is executed, the ECX register is decremented and the control jumps to the target label, until the ECX register value reaches the value zero. 326 | 327 | ``` 328 | MOV total, 0 329 | MOV CX, 5 ;execute the loop 5 times 330 | MOV SI, 1 331 | L1: 332 | ADD total, SI 333 | INC SI 334 | LOOP L1 335 | ``` 336 | --------------------------------------------------------------------------------