Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 
main.cpp
#include <stdio.h>

char GlChar = 10;
short GlShort = 20;
int GlInt = 30;
long long GlLongLong = 0x000000000FFFFFFFE;

extern char GlChar;
extern short GlShort;
extern int GlInt;
extern long long GlLongLong;

extern "C" void IntegerAddition(char a, short b, int c, long long d);

int main(int argc, char* argv[])
{
	printf("Before GlChar:     %d\n", GlChar);
	printf("       GlShort:    %d\n", GlShort);
	printf("       GlInt:      %d\n", GlInt);
	printf("       GlLongLong: %lld\n", GlLongLong);
	printf("\n");
	
	IntegerAddition(3, 5, -37, 11);
	
	printf("After  GlChar:     %d\n", GlChar);
	printf("       GlShort:    %d\n", GlShort);
	printf("       GlInt:      %d\n", GlInt);
	printf("       GlLongLong: %lld\n", GlLongLong);
	return 0;
}
integeraddition.asm
; Name:         integeraddition.asm
;
; Description:  This function demonstrates simple addition using
;               various-sized integers.
;
; Source:       Modern x86 Assembly Language Programming p.46

; These are defined in IntegerAddition.cpp

extern GlChar
extern GlShort
extern GlInt
extern GlLongLong

global IntegerAddition

section .text

; extern "C" void IntegerTypes_(char a, short b, int c, long long d);

%define a       [ebp+8]
%define b       [ebp+12]
%define c       [ebp+16]
%define dlow    [ebp+20]
%define dhigh   [ebp+24]

IntegerAddition:
    ; Function prolog
    push    ebp
    mov     ebp,esp
    ; Compute GlChar += a
    mov     al,a
    add     [GlChar],al
    ; Compute GlShort += b, note offset of 'b' on stack
    mov     ax,b
    add     [GlShort],ax
    ; Compute GlInt += c, note offset of 'c' on stack
    mov     eax,c
    add     [GlInt],eax
    ; Compute GlLongLong += d, note use of dword ptr operator and adc
    mov     eax,dlow
    mov     edx,dhigh
    add     dword[GlLongLong],eax
    adc     dword[GlLongLong+4],edx
    ; Function epilog
    pop     ebp
    ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o integeraddition.o integeraddition.asm
g++ -m32 -o integeraddition integeraddition.o main.o