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

extern "C" int SignedMinA(int a, int b, int c);
extern "C" int SignedMaxA(int a, int b, int c);
extern "C" int SignedMinB(int a, int b, int c);
extern "C" int SignedMaxB(int a, int b, int c);
extern "C" int SignedIsEQ(int a, int b);

int main(int argc, char* argv[])
{
	int a, b, c;
	int smin_a, smax_a;
	int smin_b, smax_b;
	
	// SignedMin examples
	a = 2; b = 15; c = 8;
	smin_a = SignedMinA(a, b, c);
	smin_b = SignedMinB(a, b, c);
	printf("SignedMinA(%4d, %4d, %4d) = %4d\n", a, b, c, smin_a);
	printf("SignedMinB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smin_b);
	
	a = -3; b = -22; c = 28;
	smin_a = SignedMinA(a, b, c);
	smin_b = SignedMinB(a, b, c);
	printf("SignedMinA(%4d, %4d, %4d) = %4d\n", a, b, c, smin_a);
	printf("SignedMinB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smin_b);
	
	a = 17; b = 37; c = -11;
	smin_a = SignedMinA(a, b, c);
	smin_b = SignedMinB(a, b, c);
	printf("SignedMinA(%4d, %4d, %4d) = %4d\n", a, b, c, smin_a);
	printf("SignedMinB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smin_b);
	
	// SignedMax examples
	a = 10; b = 5; c = 3;
	smax_a = SignedMaxA(a, b, c);
	smax_b = SignedMaxB(a, b, c);
	printf("SignedMaxA(%4d, %4d, %4d) = %4d\n", a, b, c, smax_a);
	printf("SignedMaxB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smax_b);
	
	a = -3; b = 28; c = 15;
	smax_a = SignedMaxA(a, b, c);
	smax_b = SignedMaxB(a, b, c);
	printf("SignedMaxA(%4d, %4d, %4d) = %4d\n", a, b, c, smax_a);
	printf("SignedMaxB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smax_b);
	
	a = -25; b = -37; c = -17;
	smax_a = SignedMaxA(a, b, c);
	smax_b = SignedMaxB(a, b, c);
	printf("SignedMaxA(%4d, %4d, %4d) = %4d\n", a, b, c, smax_a);
	printf("SignedMaxB(%4d, %4d, %4d) = %4d\n\n", a, b, c, smax_b);
	
	//Signed is equal examples
	a = -25; b = -37;
	bool d = SignedIsEQ(a, b);
	if(d){printf("%4d = %d\n", a, b);}else{printf("%4d <> %d\n", a, b);}
	a = 25; b = 25;
	d = SignedIsEQ(a, b);
	if(d){printf("%4d = %d\n", a, b);}else{printf("%4d <> %d\n", a, b);}
}
conditioncodes.asm
; Name:     conditioncodes.asm
;
; Build:    g++ -m32 -c main.cpp -o main.o
;           nasm -f elf32 -o conditioncodes.o conditioncodes.asm
;           g++ -m32 -o conditioncodes conditioncodes.o main.o
;
; Source:   Modern x86 Assembly Language Programming p.50

global  SignedMinA
global  SignedMaxA
global  SignedMinB
global  SignedMaxB
global	SignedIsEQ

section .text

%define a   [ebp+8]
%define b   [ebp+12]
%define c   [ebp+16]

; extern "C" int SignedMinA(int a, int b, int c);
;
; Description:  Determines minimum of three signed integers
;               using conditional jumps.
;
; Returns       min(a, b, c)

SignedMinA:
    push    ebp
    mov     ebp,esp
    mov     eax,a               ;eax = 'a'
    mov     ecx,b               ;ecx = 'b'
    ; Determine min(a, b)
    cmp     eax,ecx
    jle     .l1
    mov     eax,ecx             ;eax = min(a, b)
    ; Determine min(a, b, c)
.l1:
    mov     ecx,c               ;ecx = 'c'
    cmp     eax,ecx
    jle     .l2
    mov     eax,ecx             ;eax = min(a, b, c)
.l2:
    pop     ebp
    ret

; extern "C" int SignedMaxA(int a, int b, int c);
;
; Description:  Determines maximum of three signed integers
;               using conditional jumps.
;
; Returns:      max(a, b, c)

SignedMaxA:
    push    ebp
    mov     ebp,esp
    mov     eax,a               ;eax = 'a'
    mov     ecx,b               ;ecx = 'b'
    cmp     eax,ecx
    jge     .l1
    mov     eax,ecx             ;eax = max(a, b)
.l1:
    mov     ecx,c               ;ecx = 'c'
    cmp     eax,ecx
    jge     .l2
    mov     eax,ecx             ;eax = max(a, b, c)
.l2:
    pop     ebp
    ret

; extern "C" int SignedMinB(int a, int b, int c);
;
; Description:  Determines minimum of three signed integers
;               using conditional moves.
;
; Returns       min(a, b, c)

SignedMinB:
    push    ebp
    mov     ebp,esp
    mov     eax,a               ;eax = 'a'
    mov     ecx,b               ;ecx = 'b'
    ; Determine smallest value using the CMOVG instruction
    cmp     eax,ecx
    cmovg   eax,ecx             ;eax = min(a, b)
    mov     ecx,c               ;ecx = 'c'
    cmp     eax,ecx
    cmovg   eax,ecx             ;eax = min(a, b, c)
    pop     ebp
    ret

; extern "C" int SignedMaxB(int a, int b, int c);
;
; Description:  Determines maximum of three signed integers
;               using conditional moves.
;
; Returns:      max(a, b, c)

SignedMaxB:
    push    ebp
    mov     ebp,esp
    mov     eax,a               ;eax = 'a'
    mov     ecx,b               ;ecx = 'b'
    ; Determine largest value using the CMOVL instruction
    cmp     eax,ecx
    cmovl   eax,ecx             ;eax = max(a, b)
    mov     ecx,c               ;ecx = 'c'
    cmp     eax,ecx
    cmovl   eax,ecx             ;eax = max(a, b, c)
    pop     ebp
    ret

; extern "C" bool SignedIsEQ(int a, int b);
;
; Description:  Compare a against b and set AL appropriately.
;
; Returns:      True if a = b, False if not equal.

SignedIsEQ:
	push    ebp
    mov     ebp,esp
    xor     eax,eax
    mov     ecx,a
    cmp     ecx,b
    sete    al
    pop     ebp
    ret
build
g++ -m32 -c main.cpp -o main.o
nasm -f elf32 -o conditioncodes.o conditioncodes.asm
g++ -m32 -o conditioncodes conditioncodes.o main.o