; Common MASM instructions and C equivalents .386 ; Assemble instructions for 386 and below .model flat ; 32-bit code .stack 4096 ; Allocate 4K stack space .data ; Global and static variables go here c1 db 10 ; char c1 = 10; c2 db ? ; char c2; // not initialized c3 db "hello",13,10,0 ; char c3[] = "hello\r\n"; c4 db 8 dup(?) ; char c4[8]; c5 db 5 dup(3) ; char c5[5] = {3,3,3,3,3}; c6 byte ? ; char c6; // byte = db w dw 1000 ; short w = 1000; // dw = word, 2 bytes d dd 0ffffffffh ; long d = 0xffffffff; // dd = dword, 4 bytes q dq 1.2, 3.4; ; double q[2] = {1.2, 3.4}; // dq = qword, 8 bytes t dt 5.6; ; long double t = 5.6; // dt = tword, 10 bytes .code ; Code goes here _main: ; A label ; Assignment - at most one operand can be a memory location mov eax, ebx ; eax = ebx; // register to register mov ecx, 3 ; ecx = 3; // immediate to register mov eax, 'j' ; eax = 'j' // 000000a6h = 0,0,0,'j' mov edx, offset c3 ; edx = &c3; // edx points to "hello" mov ebx, [d]; ; ebx = d; // indirect forms, ebx = ffffffffh = -1 mov [edx], al ; *edx = al; // change "hello" to "jello" mov ah, [edx] ; ah = *edx; // eax = 0,0,"jj" mov ah, [edx+1] ; ah = edx[1] // eax = 0,0,"ej" mov al, [ecx+edx] ; al = *(ecx+edx); // eax = 0,0,"el" mov al, [edx+ecx*8] ; scale may be *1, *2, *4, *8 mov al, [edx+ecx*4+1] ; any 2 registers (one scaled) + constant mov [edx+ecx*4+1], al ; ... as source or destination ; Arithmetic (operands as in MOV), sets flags add eax, ebx ; eax += ebx; // 32, 16, 8 bits depending on register size) add ax, [w] ; ax += w; add [c1], al ; c1 += al; sub eax, [d] ; eax -= d; inc eax ; ++eax; inc byte ptr [edx] ; ++*(char*)edx; // explicit size of operand inc word ptr [edx] ; ++*(short*)edx; inc dword ptr [edx] ; ++*(long*)edx; dec eax ; --eax; neg eax ; eax = -eax; not eax ; eax = ~eax; and eax, ebx ; eax &= ebx; or eax, ebx ; eax |= ebx; xor eax, ebx ; eax ^= ebx; sal eax, cl ; eax <<= cl; // must shift by cl or 0-31 sar eax, cl ; (long) eax >>= cl; shr eax, cl ; (unsigned long) eax >>= cl; imul bl ; ax = al * bl; // 8 bit signed imul bx ; dx:ax = ax * bx; // 16 bit signed imul ebx ; edx:eax = eax * ebx; // 32 bit signed mul ebx ; edx:eax = eax * ebx; // 32 bit unsigned idiv ebx ; edx = eax % ebx; eax /= ebx; // 8,16,32 bit signed div ebx ; edx = eax % ebx; eax /= ebx; // ...or unsigned ; Conversion (sign extend) cbw ; ax = (short) al; // al, ax, eax only cwd ; eax = (long) ax; cdq ; edx = -(eax < 0); movsx bx, al; ; bx = (short) al; // operands as in MOV movzx bx, al; ; bx = (unsigned short) al; movsx ebx, ax; ; ebx = (long) ax; movzx ebx, ax; ; ebx = (unsigned long) ax; ; Compare (operands as in MOV) cmp eax, ebx ; Set CF, OF, SF, ZF ja l1 ; if (eax > ebx) goto l1; // unsigned compare jb l1 ; < jae l1 ; >= jbe l1 ; <= je l1 ; == jne l1 ; != jg l1 ; if (eax > ebx) goto l1; // signed compare jl l1 ; < jge l1 ; >= jle l1 ; <= jmp l1 ; goto l1; l1: ; l1: // label test eax, ebx ; // bit test jz l2 ; if ((eax & ebx) == 0) goto l2; // jz = je jnz l2 ; != // jnz = jne l2: ; Loops mov ecx, 10 ; // ecx is used as a counter l3: loop l3 ; do {} while (--ecx); ; Copy 8 bytes of array c3 to c4 cld ; clear direction flag = move forward, std = backwards mov edi, offset c4 ; pointer to destination mov esi, offset c3 ; pointer to source mov ecx, 8 ; number of elements to copy rep movsb ; memcpy(edi, esi, ecx); // also movsw, movsd ; Compare arrays, ZF set if equal mov edi, offset c4 mov esi, offset c3 mov ecx, 8 l4: cmpsb ; ZF = (*esi++ == *edi++) // also cmpsw, cmpsd loope l4 ; if (--ecx && ZF) goto l4; // or loopz ; Search array, esi points to found byte or end mov al, 'h' ; element to search for mov esi, offset c3 ; array to search mov ecx, 8 ; size of array l5: scasb ; ZF = (al == *esi++) // also scasw, scasd loopne l5 ; if (--ecx && !ZF) goto l4; // or loopnz ; Stack operations (uses esp as stack pointer) push 123; ; esp -= 4; *esp = 123; call f ; f(123); // pushes $ as return address pop ebx; ; ebx = *esp; esp += 4; ret; ; return eax; // pops to EIP f: ; void f(long) mov eax, [esp+4] ; 123 ret ; return; end _main ; // "end" identifies the entry point ; This program will assemble and run, but not do anyting: ; ; ml /c /coff ex.asm ; link32 ex.obj ; ex