0% found this document useful (0 votes)
47 views

Bài 1: #Include #Include Using Namespace STD Int Count (Int A) (Int S - Asm (Mov Ebx, Ss:a Mov Eax, 0 WHILE: CMP Ebx, 0 Je End - While Test Ebx, 1

The document contains code to count the number of 1s in a binary representation of a number and to sum the elements of an array that are divisible by 4. The Count function uses assembly instructions to count the 1s by shifting the input number and incrementing a counter for each 1 bit. The Sum function uses assembly to iterate through an array, shift each element to check for divisibility by 4, and add matching elements to a running sum total. Both functions demonstrate using inline assembly code in C++ to perform bit operations and numeric calculations directly in assembly rather than using high-level C++ constructs.

Uploaded by

Ichigo Pham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Bài 1: #Include #Include Using Namespace STD Int Count (Int A) (Int S - Asm (Mov Ebx, Ss:a Mov Eax, 0 WHILE: CMP Ebx, 0 Je End - While Test Ebx, 1

The document contains code to count the number of 1s in a binary representation of a number and to sum the elements of an array that are divisible by 4. The Count function uses assembly instructions to count the 1s by shifting the input number and incrementing a counter for each 1 bit. The Sum function uses assembly to iterate through an array, shift each element to check for divisibility by 4, and add matching elements to a running sum total. Both functions demonstrate using inline assembly code in C++ to perform bit operations and numeric calculations directly in assembly rather than using high-level C++ constructs.

Uploaded by

Ichigo Pham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bi 1:

#include <iostream>
#include<conio.h>
using namespace std;
int Count(int a)
{
int s;
__asm{
mov ebx, ss:a
mov eax,0
WHILE: cmp ebx,0
je END_WHILE
test ebx,1
je a
INC eax
a:SHR bx,1
jmp WHILE
END_WHILE:
mov s,eax
}

return s;
}

int main()
{
int a,n;
cout<<"nhap 1 so: ";
cin>>n;
a=Count(n);
cout<<"\nso bit 1 cua so "<<n<<" la "<<a;
getch();
return 0;
}
Bai 2:
#include <iostream>
#include<conio.h>
using namespace std;
int Sum(int A[], int N)

{int s=0;
__asm{
mov ebx, ss:A
mov ecx, ss:N
mov edx,0
shl cx,2
mov edi,0
FOR: cmp edi,ecx
jae END_FOR
mov eax,[ebx][edi]
shr eax,2
jc Khongchiahet
shl eax,2
add edx,eax
Khongchiahet:
add di,4
jmp FOR
END_FOR: mov s,edx
}


return s;

}

int main()
{
int b;
int a[3];
cout<<"nhap gia tri mang";
for(int i=0;i<3;i++)
{
cout<<"\na["<<i<<"]=";
cin>>a[i];
}
cout<<"\ntong so phan tu chia het cho 4 cua mang a[3] la: ";
b=Sum(a,3);
cout<<b;
getch();
return 0;
}

You might also like