Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- using namespace std;
- int invers(int);
- int main()
- {
- int n;
- cin >> n;
- cout << invers(n);
- }
- int invers(int)
- {
- /*
- Notatii:
- edi = nr
- esi = 10
- ebx = inv
- ALGORITM:
- inv=0
- while nr!=0 do
- nr/10=c rest r => inv=inv*10+r nr=c
- Cum facem impartirea:
- 1.Facem edi/esi = eax rest edx ("edi/esi" adica "mov eax,edi" + "div esi")
- 2.in ecx vom tine minte restul edx (edx e folosit by default la inmultire deci se sterge valoarea)
- rezultat=> inv=inv*10+ecx (inv=inv*10+nr%10) si edi(nr-ul)=eax ( nr=[nr/10])
- */
- _asm {
- mov edi,[ebp+8]
- mov esi,10
- mov ebx,0
- forInceput:
- cmp edi,0
- je stopFor
- mov eax,edi // mutam edi(nr-ul) in eax
- mov edx,0 // necesar ptr div
- div esi // (edx::eax)/esi = eax rest edx ( esi=10!!!)
- mov edi,eax // edi=[edi/10]
- //restul este adaugat la inv
- mov ecx,edx // tinem minte restul in ecx
- mov eax,ebx // mutam inversul in eax
- mul esi // eax*esi= edx::eax => edx=0 si eax=inv*10
- mov ebx,eax // inv = inv*10
- add ebx,ecx // inv = inv*10 + n%10
- jmp forInceput
- stopFor:
- mov eax,ebx //eax returneaza deci salvam in el nr-ul
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement