140 likes | 371 Views
COMP 2003: Assembly Language and Digital Logic. Chapter 1: Behind the Scenes of Your Code Notes by Neil Dickson. Basic Data Types. Byte #:. 0. 1. 2. 3. 4. 5. 6. 7. Bit #:. 0. 7. 8. 15. 16. 23. 24. 31. 32. 39. 40. 47. 48. 55. 56. 63. BYTE. BYTE. BYTE. BYTE. BYTE.
E N D
COMP 2003:Assembly Language and Digital Logic Chapter 1: Behind the Scenes of Your Code Notes by Neil Dickson
Basic Data Types Byte #: 0 1 2 3 4 5 6 7 Bit #: 0 ... 7 8 ... 15 16 ... 23 24 ... 31 32 ... 39 40 ... 47 48 ... 55 56 ... 63 BYTE BYTE BYTE BYTE BYTE BYTE BYTE BYTE WORD WORD WORD WORD DWORD DWORD QWORD
General Registers Byte 3 Byte 2 Byte 1 Byte 0 • DWORD registers eax ax ah al • WORD registers ecx ch cx cl edx dx dh dl • BYTE registers ebx bx bh bl (stack pointer) esp sp (base pointer) ebp bp esi si edi di
Instruction Format DoSomething: moveax,[esi];read some memory line label source operand comment mnemonic destination operand
Instruction: mov moveax,ebx ;eax = ebx moveax,[ebx] ;eax = *((DWORD*)ebx) mov[eax],ebx ;*((DWORD*)eax) = ebx moveax,12345 ;eax = 12345 moveax,12345*123 ;eax = 12345*123 mov[eax],12345 ;*(eax) = 12345 dword ptr (DWORD*)
Examples of mov moveax,ebx moveax,[ebx] eax 000918C5h eax 000918C5h ebx 00E10267h 00E10267h ebx 00E10267h movword ptr [ebx],12345 3039h ebx Memory: ... B3h 00214948h 39h 48h 30h 49h 21h 00h 33h 7Fh 26h ... Addresses: ... ... 00E10266h 00E10267h 00E10268h 00E10269h 00E1026Ah 00E1026Bh 00E1026Ch 00E1026Dh
Instruction: add addeax,ebx addeax,[ebx] add[eax],ebx addeax,12345 addeax,12345*123 adddword ptr [eax],12345 ;eax += ebx ;eax += *((DWORD*)ebx) ;*((DWORD*)eax) += ebx ;eax += 12345 ;eax += 12345*123 ;*((DWORD*)eax) += 12345
Instructions: cmp & je cmpeax,ebx jeSomePlace ;if (eax==ebx) ; gotoSomePlace for (i=0;i!=n;++i) { ... } i=0; NextElement: if (i==n) goto DoneLoop; ... ++i; goto NextElement; DoneLoop: i=0; while (i!=n) { ... ++i; }
Instructions: cmp & jne cmpeax,ebx jneSomePlace ;if (eax!=ebx) ; gotoSomePlace for (i=0;i!=n;++i) { ... } (supposing n≥1) i=0; NextElement: ... ++i; if (i!=n) goto NextElement; i=0; do { ... ++i; } while (i!=n);
Memory Addressing movebx,2 movecx,3 moveax,[ebx] moveax,[ebx+4] moveax,[ebx][4] moveax,[ebx*2] moveax,[ebx*4] moveax,[ebx*8] moveax,[ebx+ecx] moveax,[ebx+ecx*2] moveax,[ebx+ecx*4] moveax,[ebx+ecx*8] ;read DWORD at addresses 02h-05h ;addresses 06h-09h (2+4=6) ;same as previous ;addresses 04h-07h (2*2=4) ;addresses 08h-0Bh (2*4=8) ;addresses 10h-13h (2*8=16=10h) ;addresses 05h-08h (2+3=5) ;addresses 08h-0Bh (2+3*2=8) ;addresses 0Bh-0Eh (2+3*3=11=0Bh) ;addresses 0Eh-11h (2+3*4=14=0Eh)
Full Example: From C to Assembly int sum = 0; for (int i=0;i!=n;++i) { sum += myDwords[i]; } int eax = 0; int ebx = 0; NextElement: eax += edx[ebx]; ++ebx; if (ebx!=ecx) goto NextElement; (renaming variables) (supposing n≥1) int sum = 0; int i = 0; NextElement: sum += myDwords[i]; ++i; if (i!=n) goto NextElement; (supposing ecx holds number of elements & edx holds address of myDwords) moveax,0 movebx,0 NextElement: addeax,[edx+ebx*4] addebx,1 cmpebx,ecx jneNextElement
Full Example: Clearing an image for (int i=0;i!=nPixels;++i) { pBitmap[i] = BLUE; } int ebx = 0; NextPixel: edx[ebx] = BLUE; ++ebx; if (ebx!=ecx) goto NextPixel; (supposing n≥1) (renaming variables) int i = 0; NextPixel: pBitmap[i] = BLUE; ++i; if (i!=nPixels) goto NextPixel; (supposing ecx holds number of elements & edx holds address of the bitmap) movebx,0 NextPixel: movdword ptr [edx+ebx],BLUE addebx,4 cmpebx,ecx jneNextPixel