You are on page 1of 3

NAME: SYED MUNEEB ALI RIZVI

ROLL NO: 21K-4924


SEC: K
COAL LAB # 12

TASK NO 1:
CPP CODE:
#include <stdio.h>
int ThreeProd(int* arr) {
int n1 = arr[0];
int n2 = arr[1];
int n3 = arr[2];
int ans;
_asm {
mov eax, n1;
mov edx, 0;
mul n2;
mul n3;
mov ans, eax;
}
return ans;
}
int main()
{
//define variables
int arr[3];
for (int i = 0; i < 3; i++) {
printf("Enter number %d: ", i + 1);
scanf_s("%d", &arr[i]);
}// in scanf_s it is necessary to
//specifiy length
int result = ThreeProd(arr);
printf("The product of the numbers = %d", result);
return 0;
}

ASM CODE:
.686 ;Target processor. Use instructions for Pentium class machines
.MODEL FLAT, C ;Use the flat memory model. Use C calling conventions
.STACK 2048 ;Define a stack segment of 1KB (Not required for this example)
.DATA ;Create a near data segment. Local variables are declared after

;this directive (Not required for this example)


.CODE ;Indicates the start of a code segment.
; nothing needed here
END
TASK NO 2:
CPP CODE:
#include <stdio.h>
extern "C" void GCD(int, int);
int main()
{
//define variables
int arr[2];
for (int i = 0; i < 2; i++) {
printf("Enter number %d: ", i + 1);
scanf_s("%d", &arr[i]);
}
GCD(arr[0], arr[1]);
int gcd;
_asm {
mov gcd,eax
}
printf("The GCD of %d and %d is: %d", arr[0], arr[1], gcd);
return 0;
}

ASM CODE:
.686 ;Target processor. Use instructions for Pentium class machines
.MODEL FLAT, C ;Use the flat memory model. Use C calling conventions
.STACK 2048 ;Define a stack segment of 1KB (Not required for this example)
.DATA ;Create a near data segment. Local variables are declared after

;this directive (Not required for this example)


.CODE ;Indicates the start of a code segment.
GCD PROC
LOCAL A:DWORD,B:DWORD
mov eax,[ebp+8]
mov A,eax
mov eax,[ebp+12]
mov B,eax
cmp A,0
jz firstEnd
cmp B,0
jz secondEnd
mov eax,A
cmp eax,B
jz thirdEnd
jg firstRecurse
jl secondRecurse
firstEnd:
mov eax,B
pop ebp
ret
jmp endProgram
secondEnd:
mov eax,A
pop ebp
ret
jmp endProgram
thirdEnd:
mov eax,A
ret
jmp endProgram
firstRecurse:
mov eax,A
sub eax,B
mov A,eax
push B
push A
call GCD
jmp endProgram
secondRecurse:
mov eax,B
sub eax,A
mov B,eax
push B
push A
call GCD
endProgram:
ret
GCD ENDP
END

You might also like