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/14] (current)
leszek
Line 30: Line 30:
 | 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>​
  
  
notes/assembly.1560479345.txt.gz ยท Last modified: 2019/06/13 by leszek