Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 14

// asm_pen.cpp : This file contains the 'main' function.

Program execution begins


and ends there.
//

#include "pch.h"
#include <iostream>
#include <stdio.h>

using namespace std;

void f2(char c[13]) {


_asm {
mov eax, [ebp + 8]
or byte ptr[eax], 32
}
}
int main()
{
char s[]='ana';
f2(s);
}
/*
void diagPP0(int *, int n) {
int numar;
_asm {
mov edi, [ebp + 8]
mov edx, [ebp + 12]
lea ebx, [edx * 4 + 4]

mov ecx, 0 //i=0


_while:
cmp ecx, edx //if i<n
jge _gata
mov dword ptr[edi], 0
add edi, ebx
inc ecx
jmp _while
_gata :

}
}
void diagSEC0(int *, int n) {
int numar;
int asd;
_asm {
mov ebx, [ebp+8]
mov ecx, [ebp+12]
lea eax, [ecx*4-4]
add ebx, eax
mov esi, 0 // i=0;
_while:
cmp esi, ecx
jge endWhile

mov dword ptr[ebx], 0


add ebx, eax

inc esi
jmp _while
endWhile:

}
}

unsigned nrBits(int)
{
_asm
{
MOV ebx, [ebp + 8]
MOV EAX, 0
start_while:
CMP ebx, 0
JE stop_while

MOV ecx, ebx;


AND ecx, 1
CMP ecx, 0
JE not_increment
INC eax

not_increment :
SHR ebx, 1
JMP start_while
stop_while :
}

unsigned isPrime(int)
{
_asm
{
MOV ebx, [ebp + 8]
CMP ebx, 2
je prim
jl not_prim

MOV edx, 0
MOV ecx, 2
MOV eax, ebx
DIV ecx
MOV esi, eax

for_start :
cmp ecx, esi
JG for_stop

MOV edx, 0
MOV eax, ebx
DIV ecx
CMP edx, 0
JE not_prim

INC ecx
for_stop :

prim:
MOV eax, 1
jmp endu
not_prim :
MOV eax, 0
jmp endu

endu :
}
}
int palindrom(unsigned int)
{
_asm
{
MOV ebx, [ebp + 8]
MOV esi, ebx

MOV ecx, 0
MOV edi, 10

while_start:
cmp esi, 0
je while_stop

MOV edx, 0
MOV eax, esi
DIV edi
MOV esi, eax

MOV[ebp + 12 + ecx * 4], edx


inc ecx

jmp while_start
while_stop :

MOV eax, 0

MOV esi, 0
for_start :
CMP esi, ecx
JGE for_stop
MUL edi
ADD eax, [ebp + 12 + esi * 4]
INC esi

jmp for_start
for_stop :

CMP eax, ebx


JNE not_pali

is_pali :
mov eax, 1
jmp _end

not_pali :
mov eax, 0
jmp _end
_end :
}
}
int invers(unsigned int)
{
_asm
{
MOV ebx, [ebp + 8]
MOV esi, ebx

MOV ecx, 0
MOV edi, 10

while_start:
cmp esi, 0
je while_stop

MOV edx, 0
MOV eax, esi
DIV edi
MOV esi, eax

MOV[ebp + 12 + ecx * 4], edx


inc ecx

jmp while_start
while_stop :

MOV eax, 0

MOV esi, 0
for_start :
CMP esi, ecx
JGE for_stop
MUL edi
ADD eax, [ebp + 12 + esi * 4]
INC esi

jmp for_start
for_stop :

}
}
int powerOf2(int, int*)
{
_asm
{
MOV eax, 0
MOV ebx, [ebp + 12]
MOV ecx, [ebp + 8]

for_start:
cmp ecx, 0
JLE for_stop
MOV edi, [ebx + 4 * ecx - 4]
MOV esi, edi
DEC edi
AND esi, edi
CMP esi, 0
JNE not_power_of_2

power_of_2 :
INC eax

not_power_of_2 :

DEC ecx
jmp for_start
for_stop :
_end:
}
}
int progresie_aritmetica(int, int*) {
_asm {

MOV ebx, [ebp + 12]


MOV ecx, [ebp + 8]

cmp ecx, 3
JL not_progresie

MOV edx, [ebx + 4]


SUB edx, [ebx]

for_start:
CMP ecx, 1
JL for_stop

MOV edi, [ebx + 4 * ecx - 4]


SUB edi, [ebx + 4 * ecx - 8]

CMP edi, edx


JNE not_progresie

DEC ecx
for_stop :

progresie:
MOV eax, 1
JMP _end
not_progresie :
MOV eax, 0
JMP _end
_end :
}
}

void print1() {
cout << "DA";
}

void print2() {
cout << "NU";
}

/*int main() // 1
{
unsigned char p = 0;
_asm {
mov eax, main
}
for (int i = 0; i < 10; i++) {
_asm {
mov p, byte[eax]
inc eax
}
printf("%X ", p);
}
}*/

/*int main() // 2
{

int x, y;
x = 0;
_asm {
mov eax, x
cmp eax, 0
je el
mov dword ptr y, 7
jmp els
el:
mov dword ptr y, 3
els:

}
*/
/*
byte ---> 1 byte ah/al
word ---> 2 bytes ax
dword ---> 4 bytes eax
qword ---> 8 bytes rax
jmp-uri
j ---> jump if
e ---> equal
ne ---> not equal
g ---> greater
l ---> lower
ge ---> greater or equal
le ---> lower or equal
z ---> zero
nz ---> not zero
a ---> greater without sign
ae ---> greater equal without sign
b ---> lower without sign
be ---> lower equal without sign
$ ---> pozitia curenta
lea ---> low effective address, get the address of a variable
*/
//cout << y << endl;
/*
int s, n;
cin >> n;
_asm {
mov eax, 0
mov ecx, 0
beginfor:
cmp ecx, n
jge endfor
add eax, ecx
inc ecx
jmp beginfor
endfor:
mov s, eax
}
cout << s << endl;
*/
/*
char s[100];
int n;
cin >> s;
_asm {
mov ecx, 0
lea eax, s;
beginwhile:
mov bl, [eax + ecx]
cmp bl, 0
je endwhile
inc ecx
jmp beginwhile
endwhile:
mov n, ecx
}
cout << n << endl;
*/

//suma nr. pare

/*int x[] = { 1, 2, 3, 4, 5, 6 };
int n = 6, s;
_asm {
mov ecx, 0
mov eax, 0
mov edx, 0
mov edi, 2
mov s, 0
lea ebx, x
beginwhile :
cmp ecx, n
je endwhile
mov eax, DWORD PTR[ebx + 4 * ecx]
div edi
cmp edx, 0
jne endif
mov eax, DWORD PTR[ebx + 4 * ecx]
add s, eax
mov edx, 0
endif:
inc ecx
jmp beginwhile
endwhile :
}
cout << s << endl;
return 0;
}
*/

// 3

int sum(int, int) {


_asm {
mov eax, DWORD PTR[ebp + 8]
add eax, DWORD PTR[ebp + 12]
}
}

int len(char*) {
_asm {
mov ebx, [ebp + 8]
mov eax, 0
beginwhile:
mov cl, BYTE PTR[ebx + eax]
cmp cl, 0
je endwhile
inc eax
jmp beginwhile
endwhile :
}
}

int toUpperLower(char*, int) {


_asm {
mov ebx, [ebp + 8]
push ebx
call len
add esp, 4
mov ecx, eax
dec ecx
mov eax, 0
mov ebx, [ebp + 12]
cmp ebx, 1
je TO_LOWER
mov ebx, [ebp + 8]
whileUpper:
cmp ecx, -1
je endwhileUpper
mov dl, BYTE PTR[ebx + ecx]
cmp dl, 'a'
jl endifs
cmp dl, 'z'
jg endifs
inc eax
sub dl, BYTE PTR 32
mov BYTE PTR[ebx + ecx], dl
endifs :
dec ecx
jmp whileUpper
endwhileUpper :
jmp END_FUNCTION
TO_LOWER :
mov ebx, [ebp + 8]
whileLower :
cmp ecx, -1
je endwhileLower
mov dl, BYTE PTR[ebx + ecx]
cmp dl, 'A'
jl endifs2
cmp dl, 'Z'
jg endifs2
inc eax
add dl, 32
mov BYTE PTR[ebx + ecx], dl
endifs2 :
dec ecx
jmp whileLower
endwhileLower :
jmp END_FUNCTION
END_FUNCTION :

}
}
/*
int main()
{
/* int a = 5, b = 10, s;
_asm {
push b
push a
call sum
add esp, 8
mov s, eax
}
std::cout << s << std::endl;

char stri[] = "ANA ARE MeE";


std::cout << toUpperLower(stri, 1) << " " << stri << std::endl;
return 0;
}*/

// 4

void pare(int, int)


{
_asm {
XOR eax, eax
XOR edx, edx
MOV eax, dword ptr[ebp + 12]
DIV dword ptr[ebp + 8]

ADD eax, edx


XOR edx, edx
MOV ecx, 2
DIV ecx
CMP edx, 0
JE par
JNE impar

par :
MOV eax, 1
JMP _end
impar :
MOV eax, 0

JMP _end
_end :
}
}

void fill(int *, int, int) {


_asm {
mov edx, 0
mov eax, DWORD PTR[ebp + 12]
mov ecx, DWORD PTR[ebp + 16]
mul ecx
mov ecx, 0
mov edx, DWORD PTR[ebp + 8]

startwhile:
cmp ecx, eax
je endwhile

mov ebx, ecx


inc ebx

mov DWORD PTR[edx + 4 * ecx], ebx

inc ecx
jmp startwhile
endwhile :
}
}

int sum(int*, int, int) {


_asm {
mov edx, 0
mov eax, DWORD PTR[ebp + 12]
mov ecx, DWORD PTR[ebp + 16]
mul ecx
mov ecx, 0
mov ebx, DWORD PTR[ebp + 8]
mov edx, eax
mov eax, 0
startwhile:
cmp ecx, edx
je endwhile
add eax, DWORD PTR[ebx + 4 * ecx]
inc ecx
jmp startwhile
endwhile :
}
}
const char *c = "%d";

void InmultireaUnuiVectorCuOConstanta(int *p, int l, int val)


{
int i = 0;
_asm
{
multiply:
mov ecx, i
cmp ecx, l
jge finish

mov eax, p
mul val
push eax
push c
call printf
pop eax
pop eax
inc i
add p, 4
jmp multiply

finish :
mov l, eax
xor eax, eax

}
}

/*int main()
{
int m[4][4];
fill((int*)m, 4, 4);
diagSEC0((int*)m, 4);
for (int i = 0; i <= 3; i++){
for (int j = 0; j <= 3; j++)
cout << m[i][j] << " ";
cout << endl;
}

}*/
int main()
{
int v[] = { 1, 2, 3, 4, 5 };
cout << powerOf2(5, v);
return 0;
}

// 5

struct MyStruct {
char x; //0
char a; //1
int b; //4
short c; //8
};

void fill(MyStruct*, char, char, int, short) {


__asm {
mov ebx, [ebp + 8]
mov eax, [ebp + 12]
mov[ebx], al
mov eax, [ebp + 16]
mov[ebx + 1], al
mov eax, [ebp + 20]
mov[ebx + 4], eax
mov eax, [ebp + 24]
mov[ebx + 8], eax
}
}

struct Countt {
short nrLitere; //0
int nrCifre; //4
int nrTotal; //8
};

void Compute(Countt*, char*) {


_asm {
mov ebx, [ebp + 12]
mov eax, 0
mov ecx, 0
mov edx, 0
startwhile:
cmp[ebx + ecx], 0x0
je endwhile

cmp[ebx + ecx], 'a'


jl else_caracter_mic
cmp[ebx + ecx], 'z'
jg else_caracter_mic
inc eax

else_caracter_mic :
cmp[ebx + ecx], 'A'
jl else_caracter_mare
cmp[ebx + ecx], 'Z'
jg else_caracter_mare
inc eax

else_caracter_mare :
cmp[ebx + ecx], '0'
jl else_numar
cmp[ebx + ecx], '9'
jg else_numar
inc edx
else_numar :

inc ecx
jmp startwhile
endwhile :
mov ebx, [ebp + 8]
mov[ebx], ax
mov[ebx + 4], edx
mov[ebx + 8], ecx
}
}

/*void Compute2(Count*, char*, char*) {


_asm {
mov eax, [ebp + 12]
mov ebx, [ebp + 16]
mov ecx, 0
push 0x0
push 0x0
push 0x0
startparcurgere:
cmp[eax + ecx], 0
je endparcurgere
cmp[ebx + ecx], 0
je endparcurgere
mov el, dword ptr[ebx + ecx]
cmp el, [eax + ecx]
jne endegalitate
mov edi, [esp + 12]
inc edi
mov[esp + 12], edi
cmp esi, 'a'
jl else_caracter_mic
cmp esi, 'z'
jg else_caracter_mic
mov edi, [esp]
inc edi
mov[esp], edi
else_caracter_mic :
cmp esi, 'A'
jl else_caracter_mare
cmp esi, 'Z'
jg else_caracter_mare
mov edi, [esp]
inc edi
mov[esp], edi
else_caracter_mare :
cmp esi, '0'
jl else_numar
cmp esi, '9'
jg else_numar
mov edi, [esp + 4]
inc edi
mov[esp + 4], edi
else_numar :
endegalitate:
inc ecx
jmp startparcurgere
endparcurgere :
pop eax
pop edx
pop ecx
mov ebx, [ebp + 8]
mov[ebx], ax
mov[ebx + 4], edx
mov[ebx + 8], ecx
}
}
*/
/* int main()
{
MyStruct m;
fill(&m, '1', '2', 3, 4);
std::cout << m.x << " " << m.a << " " << m.b << " " << m.c << std::endl;
Count c;
char a[] = "Ana are mere123";
char b[] = "Ana";
Compute2(&c, a, b);
std::cout << c.nrCifre << " " << c.nrLitere << " " << c.nrTotal << std::endl;
}
*/

You might also like