Académique Documents
Professionnel Documents
Culture Documents
h>
#define LEAST_SEMNIF 55607
#define SEMNIF 1
//Sa se scrie codul in limbaj de asamblare care calculeaza suma: 1+2+3+...+n, unde n = 92682
//Atentie, aceasta suma nu se poate reprezenta folosind doar 32 de biti.
void main(){
int n;
n = 92682;
int least_semnif,semnif;
_asm{
ov eax, n
ov ebx, eax
nc ebx
ul ebx
hr eax,1
ov ebx, edx
hr edx,1
hl ebx,31
r eax,ebx
ov semnif,edx
ov least_semnif, eax
}
if( least_semnif == LEAST_SEMNIF && SEMNIF == semnif){
printf("Ok!\n");
}else{
printf("Failed! Your result is: %d*pow(2,32)+%d\n",semnif, least_semnif);
}
}
#include <stdio.h>
//Sa se scrie codul in limbaj de asamblare care inverseaza bitii unui numar
void main(){
har number;
umber = 140;
asm{
mov al,number
xor cl,cl
mov bl,al
and bl,00000001b
shl bl,7
or cl, bl
mov bl,al
and bl,00000010b
shl bl,5
or cl, bl
mov bl,al
and bl,00000100b
shl bl,3
or cl, bl
mov bl,al
and bl,00001000b
shl bl,1
or cl, bl
mov bl,al
and bl,00010000b
shr bl,1
mov bl,al
and bl,00100000b
shr bl,3
mov bl,al
and bl,01000000b
shr bl,5
or cl, bl
mov bl,al
and bl,10000000b
shr bl,7
or cl, bl
mov number,cl
#include <stdio.h>
//Completati exemplul urmator astfel incat functia max sa returneze maximun dintre a si b
int max(int a, int b){
int maxim;
_asm{
//completati
mov eax, a
mov ebx, b
cmp eax,ebx
jg a_is_greater
mov maxim, ebx
jmp _done
a_is_greater:
mov maxim,eax
_done:
}
return maxim;
}
void main(){
int a,b;
printf("a = ");
scanf("%d",&a);
printf("b = ");
scanf("%d",&b);
printf("MAX(a,b) = %d",max(a,b));
}
#include <stdio.h>
//Sa se scrie codul in limbaj de asamblare care oglindeste bitii unui numar
void main(){
nsigned int number;
umber = 140;
asm{
/* Completati */
mov ebx, number
xor eax,eax
mov ecx,0
_loop:
cmp ecx,32
jz _done
shl eax,1
shr ebx,1
jnc _next_bit
or eax,1
_next_bit:
inc ecx
jmp _loop
_done:
mov number, eax
#include <stdio.h>
//Calculul factorialului unui numar - recursiv
unsigned int fact_rec(unsigned int nr){
_asm{
mov esi, [ebp+8] //se pune nr in esi
cmp esi, 2
//comparare nr cu 2
jnb apel_recursiv //Daca nr >= 2, se face saltul, altfel:
mov eax, 1
// se returneaza 1
jmp gata //gata
apel_recursiv:
dec esi // nr-1
push esi
call fact_rec
add esp,4 //eax = fact(nr-1)
mov esi, [ebp+8] // esi = nr
mul esi //return fact(nr-1)*nr
gata:
}
}
void main(){
int nr,fact;
printf("nr = ");
scanf("%u",&nr);
_asm{
push nr
call fact_rec
add esp,4
mov fact, eax
}
printf("%u! = %u",nr,fact);
}
#include <stdio.h>
//interschimbati valorile variabilelor a si b
void swap (int *a, int *b)
{
_asm{
mov eax,[ebp+8]//&a
mov ebx,[ebp+12]//&b
mov ecx,[eax]//a
mov edx,[ebx]//b
mov [eax],edx
mov [ebx],ecx
}
}
void main()
{
int a=2, b=3;
swap(&a,&b);
printf("%d %d", a, b);
}
#include <stdio.h>
//Calculati suma elementelor pare dintr-un vector.
int suma_pare_vector (int *, int )
{
_asm
{
mov esi,[ebp+8]//v
mov edx,[ebp+12]//nr
xor ecx,ecx//index
xor eax,eax//suma = 0
_while:
cmp ecx,edx
jz _final
mov ebx, [esi]
test ebx,1
jnz _impar
//par
add eax,ebx
_impar:
add esi,4
inc ecx
jmp _while
_final:
}
}
void main()
{
int v[5]={5,1,2,3,6};
int *p=v;
int s;
_asm{
push 5
push p
call suma_pare_vector
add esp,8
mov s, eax
}
#include <stdio.h>
#include <malloc.h>
//Construiti matricea unitate (1 pe diagonala, 0 in rest)
void matrice_unitate(int **, int )
{
_asm{
mov esi,[ebp+8]//mat
mov edx,[ebp+12]//nr
xor ecx,ecx//index
_while:
cmp ecx,edx
jz _finish
mov edi,[esi]
xor eax,eax//index in vectorul de int-uri
_while_2:
cmp eax,ecx
jz _finish_while_2
mov dword ptr [edi],0
inc eax
add edi,4
jmp _while_2
_finish_while_2:
//suntem pe diagonala principala
mov dword ptr [edi],1
inc eax
add edi,4
//continuam pe linia curenta
_while_3:
cmp eax,edx
jz _finish_while_3
mov dword ptr [edi],0
inc eax
add edi,4
jmp _while_3
_finish_while_3:
inc ecx
add esi,4
jmp _while
_finish:
}
}
void main()
{
int n=5,i;
int **mat;
mat = (int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++){
mat[i] = (int*)malloc(n*sizeof(int));
}
_asm
{
push n
push mat
call matrice_unitate
add esp,8
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
printf("%d ", mat[i][j]);
printf ("\n");
}
#include <iostream>
using namespace std;
struct Str1{
char c;
short s;
int i;
};
struct Str2{
char c;
int i;
short s;
};
void generate(char,short, int, Str1*)
{
_asm{
mov esi,[ebp+20]
mov eax, [ebp+16]
mov [esi+4],eax
mov ax, [ebp+12]
mov [esi+2],ax
mov al, [ebp+8]
mov [esi],al
}
}
void generate_2(char,short, int, Str2*)
{
_asm{
mov esi,[ebp+20]
mov eax, [ebp+16]
mov [esi+4],eax
mov ax, [ebp+12]
mov [esi+8],ax
mov al, [ebp+8]
mov [esi],al
}
}
void main()
{
tr1* s1 = new Str1;
tr2* s2 = new Str2;
asm{
push s1
push 4
push 3
push 'a'
call generate
add esp,16
push s2
push 2
push 1
push 'b'
call generate_2
add esp,16
}
printf("%d %d %c\n",s1->i,s1->s,s1->c);
printf("%d %d %c\n",s2->i,s2->s,s2->c);
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include<string.h>
unsigned nrBits( int ){
_asm{
mov eax,0
mov ebx, [ebp+8]
_while:
shl ebx,1
jnc next
inc eax
jmp _while
next:
jz finish
jmp _while
finish:
}
}
unsigned nrBits2( int ){
_asm{
mov eax,0
mov ebx, [ebp+8]
cmp ebx,0
jz finish
do_while:
inc eax
mov edx,ebx
dec edx
and ebx,edx
jz finish
jmp do_while
finish:
}
}
unsigned isPrime( int ){
_asm{
push [ebp+8]
call nrBits
add esp,4
mov ebx,eax
shr ebx,1
mov ecx,2
mov esi,eax
_while:
cmp ecx,ebx
jg prime
mov eax,esi
start_compare:
cmp esi,edi
jnz not_palindrom
mov eax,1
not_palindrom:
}
}
unsigned short powerOf2( int , int* ){
_asm{
mov esi,[ebp+12]
mov ecx,[ebp+8]
xor ax,ax
_loop:
dec ecx
cmp ecx,0
jl finish
mov edi,[esi+4*ecx]
cmp edi,0
jz _loop
mov ebx,edi
dec ebx
and ebx,edi
jnz _loop
inc ax
jmp _loop
finish:
}
}
int progresie_aritmetica(int, int*){
_asm{
mov esi,[ebp+12]
mov edi,[ebp+8]
push ebp
sub esp,4
cmp edi,3
jl not_pa
jz is_pa
mov ebx,[esi+12]
mov [esp],ebx
sub ebx, [esi+8]
mov ecx,3
_loop:
add ecx,2
cmp ecx,edi
jge is_pa
mov ebp,2
inner_loop:
mov eax, ecx
shr eax,1
cmp ebp,eax
jge is_prime
mov eax,ecx
xor edx,edx
div ebp
cmp edx,0
jz _loop
inc ebp
jmp inner_loop
is_prime:
mov eax,[esp]
mov edx,[esi+4*ecx]
add eax,ebx
mov [esp],edx
cmp eax,edx
jz _loop
not_pa:
xor eax,eax
jmp finish
is_pa:
mov eax,1
finish:
add esp,4
pop ebp
}
}
int toUpper(char* ){
_asm{
mov esi,[ebp+8]
xor eax,eax
_loop:
mov bl,[esi]
cmp bl,0
jz finish
cmp bl,'a'
jb _next
cmp bl,'z'
ja _next
and byte ptr [esi], 11011111b
inc eax
_next:
inc esi
jmp _loop
finish:
}
}
int nrTok( char* ){
_asm{
mov esi,[ebp+8]
xor eax,eax
xor ecx,ecx
xor edx,edx
_loop:
mov bl,[esi]
cmp bl,0
jz finish
cmp bl,'.'
jz sep
cmp bl,' '
jz sep
cmp bl,'\t'
jz sep
cmp bl,','
jz sep
cmp bl,';'
jz sep
cmp bl,'('
jz sep
cmp bl,')'
jz sep
mov ecx,1
xor edx,edx
inc esi
jmp _loop
sep:
cmp ecx,1
jnz not_started
inc eax
xor ecx,ecx
not_started:
mov edx,1
inc esi
jmp _loop
finish:
cmp edx,1
jz _exit
cmp ecx,1
jnz _exit
inc eax
_exit:
}
}