Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Converting C++ algorithim to assembly language

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
MaximuS
I post too much
Reputation: 3

Joined: 05 Apr 2007
Posts: 3212
Location: ......

PostPosted: Tue Feb 17, 2015 2:52 am    Post subject: Converting C++ algorithim to assembly language Reply with quote

The 4 loops is kinda confusing, I am a beginner at assembly. Please help. this is what I have so far
I am trying to convert this algorithim written in C++
into the assembly language.
Please help me if possible, and help me understand what codes to write as well please. Much appreciated.
Sorry for asking so much,


Code:

    ;   h = 1;
    ;       while ( (h*3+1) < a.length) {
    ;       h = 3 * h + 1;
    ;   }
    mov dword [h], 1
    whileLoop1:
       mov eax, dword [dThree]
       mul dword [h]
       inc eax               ;(h*3*1)
       cmp eax, dword [length] ; while (x) < length
       jge exitwhileLoop1
       mov dword [h], eax  ; h = 3 * h + 1
       jmp whileLoop1
    exitwhileLoop1:
    ;       while( h > 0 )
    ;            {
    ;           for (i = h-1; i < a.length; i++) {
    ;               tmp = a[i];
    ;               j = i;
    ;               for( j = i; (j >= h) && (a[j-h] > B); j -= h) {
    ;                   a[j] = a[j-h];
    ;               }
    ;               a[j] = tmp;
    ;           }
    ;           h = h / 3;
    ;       }
    whileLoop2:
    cmp dword [h], 0
    jg whileLoop2
   
   
    forLoop1:
   
    mov eax, dword [i]
    forLoop2:
    mov dword [j], eax ;j = i
    cmp dword [j], dword [h]
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25785
Location: The netherlands

PostPosted: Tue Feb 17, 2015 3:09 am    Post subject: This post has 1 review(s) Reply with quote

it's just a for loop, inside a for loop, inside a while loop.
if you wish you can split up every loop into a subroutine called by the other loop to make it more clear to you

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
MaximuS
I post too much
Reputation: 3

Joined: 05 Apr 2007
Posts: 3212
Location: ......

PostPosted: Tue Feb 17, 2015 3:17 am    Post subject: Reply with quote

; for( j = i; (j >= h) && (a[j-h] > B); j -= h) {
; a[j] = a[j-h];
; }


This part is really messing me up how to implement.
Can you show me how you would do that?
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25785
Location: The netherlands

PostPosted: Tue Feb 17, 2015 6:31 am    Post subject: Reply with quote

for a general idea:
a for loop does a check before each iteration instead of after it. It runs as long as the statement in the middle entry equals true

let's assume you assign each variable a register
so: h=ebx, i=ecx, j=edx, a=esi(address of first element of a) a.length=edi
(eax=temp var)

and assuming that B is a constant variable (uppercase)

So, what this code would do then is:
Code:

mov edx,ecx ;initializer of the for loop (j=i )
loop:
;check the state ( (j >= h) && (a[j-h] > B) )

cmp edx,ebx ;cmp j,h
jl endloop ; if j<h go to endloop

mov eax,edx ;eax get the value of j
sub eax,ebx ;sub eax with h: eax is now j-h
cmp [esi+eax],B ;compare the value at a[j-h] with constant B
jle endloop ; if a[j-h] is smaller or equal to B exit the loop

;do stuff inside the loop body (a[j] = a[j-h])
mov eax,[esi+eax] ;eax already contains j-h, so now get the value a[j-h]
mov [esi+edx],eax ; a[j]([esi+edx])=a[j-h] (eax)


;next iteration  (j -= h : decrease j with the value of h )
sub edx,ebx ; decrease j (edx) with h (ebx)
jmp loop

endloop:


_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
MaximuS
I post too much
Reputation: 3

Joined: 05 Apr 2007
Posts: 3212
Location: ......

PostPosted: Tue Feb 17, 2015 12:07 pm    Post subject: Reply with quote

Here's what I ended up doing, thanks for all your help Dark Byte.

Code:

whileLoop2:
cmp dword [h], 0                  ; while( h > 0 )
jle exitwhileLoop2                  ; if h is not > 0 then exit
   ;---
   mov r8d, dword [h]               ;;
   dec r8d                        ;; h - 1
   mov dword [i], r8d               ;; (i = h - 1;
   forLoop1:                     ;; for loop check
      mov esi, dword [i]            ;;
      cmp esi, dword [len]         ;; i < length;
      jg exitforLoop1               ;;
      mov r10d, dword [lst+esi*4]      ; lst[i]
      mov dword [tmp], r10d         ; tmp = lst[i]
      mov dword [j], esi            ; j = i
      ;---
      mov r8d, dword [i]
      mov r9d, dword [h]
      forLoop2:                  ;; for loop check
         mov dword [j], r8d         ;; j = i
         cmp dword [j], r9d         ;;
         jl exitforLoop2             ;; for(j = i; (j >= h)
         mov r10d, dword [j]         ;;
         sub r10d, dword [h]         ;; j - h
         mov r11d, dword [tmp]      ;;
         cmp dword [lst+r10d*4], r11d;; && (lst[j-h] > tmp);
         jl exitforLoop2            ;;
         mov dword [j], r10d         ;; j = j-h)
         mov ebx, dword [j]         ;
         sub ebx, dword [h]         ; j - h
         mov r12d, dword [lst+ebx*4] ;
         mov r10d, dword [j]         ;
         mov dword [lst+r10d*4], r12d;lst[j] = lst[j-h]
         jmp forLoop2
      exitforLoop2:
   
   mov eax, dword [tmp]            ;
   mov ebx, dword [j]               ;
   mov dword [lst+ebx*4], eax         ; lst[j] = tmp
   inc esi                        ; i++)
   jmp forLoop1                  ;
   exitforLoop1:
mov eax, dword [h]                  ;
div dword [dThree]                  ; h / 3
mov dword [h],eax                  ; h = h /3
jmp whileLoop2
exitwhileLoop2:
Back to top
View user's profile Send private message MSN Messenger
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Fri Feb 20, 2015 5:12 am    Post subject: Reply with quote

BTW, pretty much every C/C++ compiler out there can compile to commented ASM. Hands-down, that's the easiest way to convert an algorithm from C/C++ to ASM.
_________________
A nagy kapu mellett, mindig van egy kis kapu.
----------------------
Come on...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites