User Tools

Site Tools


notes:assembly

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:assembly [2019/06/13]
leszek [Terminology]
notes:assembly [2019/06/25]
leszek [Links]
Line 21: Line 21:
 Data types supported by the x86 platform: Data types supported by the x86 platform:
  
-^  Data Type  ^  Bits  ^ +^  Data Type  ^  C++ Type  ^  Bits  ^ 
-| Byte | 8 | +| Byte | char | 8 |  
-| Word | 16 | +| Word | short | 16 | 
-| Doubleword | 32 | +| Doubleword ​| int | 32 | 
-| Quadword | 64 | +| Quadword ​| long long | 64 | 
-| Quintword | 80 | +| Quintword ​| 80 | 
-| Double Quadword | 128 | +| Double Quadword ​| 128 | 
-| Quad Quadword | 256 |+| Quad Quadword ​| 256 |
  
 +
 +===== Examples =====
 +
 +==== Calculate Sum ====
 +
 +<code asm>
 +; Produce code for the flat memory model.
 +; Use C-style names for public symbols.
 +.MODEL FLAT, C
 +
 +; Memory block that contains executable code.
 +.CODE
 +
 +; The beginning of the procedure.
 +AsmCalcSum PROC
 +
 +; Function Prolog:
 +    push ebp                    ; save the EBP register on the stack
 +    mov ebp,​esp ​                ; copy ESP to EBP i.e., set EBP as the stack frame pointer
 +                                ; this enables access to the function'​s arguments
 +
 +; ESP always points to the stack'​s top-most item
 +; EBP is used as a base pointer to access data on the stack
 +
 +; Stack:
 +; High Memory ...
 +;             [ c ] = [ebp + 16]
 +;             [ b ] = [ebp + 12]
 +;             [ a ] = [ebp + 8]
 +;             [ret] = [ebp + 4] contains the return address
 +; Low Memory ​ [ebp] contains the old value of EBP pushed on the stack using push ebp
 +;                   this is the stack'​s top-most item pointed by ESP and EBP (because of mov ebp,esp)
 +
 +; Load the argument values.
 +    mov eax,[ebp + 8]           ; a --> EAX
 +    mov ecx,[ebp + 12]          ; b --> ECX
 +    mov edx,[ebp + 16]          ; c --> EDX
 +
 +; Calculate the sum.
 +    add eax,​ecx ​                ; eax += ecx
 +    add eax,​edx ​                ; eax += edx
 +
 +; An x86-32 assembly function must use EAX to return a 32-bit
 +; integer to its calling function.
 +
 +; Function Epilog:
 +    pop ebp                     ; restore the caller'​s stack frame pointer
 +    ret
 +
 +; The end of the procedure.
 +AsmCalcSum ENDP
 +
 +; The end of statements in the file.
 +    END
 +</​code>​
 +
 +<code c>
 +#include <​stdio.h>​
 +
 +extern "​C"​ int AsmCalcSum(int a, int b, int c);
 +
 +void CalcSum()
 +{
 +    int a = 17, b = 11, c = 14;
 +    int sum = AsmCalcSum(a,​ b, c);
 +    printf("​a = %d, b = %d, c = %d\n", a, b, c);
 +    printf("​sum = %d\n\n",​ sum);
 +}
 +</​code>​
  
  
Line 64: Line 133:
 ===== Links ===== ===== Links =====
  
 +  * [[https://​web.itu.edu.tr/​kesgin/​mul06/​intel/​index.html|Intel Instruction Set Pages]]
   * [[https://​www.cs.virginia.edu/​~evans/​cs216/​guides/​x86.html|x86 Assembly Guide]]   * [[https://​www.cs.virginia.edu/​~evans/​cs216/​guides/​x86.html|x86 Assembly Guide]]
   * [[http://​www.cs.virginia.edu/​~evans/​cs216/​guides/​vsasm.html|Guide to Using Assembly in Visual Studio .NET]]   * [[http://​www.cs.virginia.edu/​~evans/​cs216/​guides/​vsasm.html|Guide to Using Assembly in Visual Studio .NET]]
notes/assembly.txt · Last modified: 2019/06/25 by leszek