Jump to content

C++ to ASM... PLZ HELP!


S.P.A.R.K.Y.

Recommended Posts

hey Hackers

i need to flip a c++ program segment over, into assembly code tomorrow as part of a task in class

im kinda lost in the battle..  :-(

I just came from the guy who set it up, and he set the rulez:

No chatting to people in the room.  (avoiding direct contact with class members)

No e-mail. (avoiding direct contact with class members)

Take all the notes that you can get, he doesn't care. (exsamples from class, google, etc.. anything) :-)

Forums are also legal  :razz: :idea:

So, i want to know if i can post here and sumone help me with the translation..

I hav some translated code wich i'm going to use, with C-code in comment next to the assembly code.

so that will help a lot.

i'll i think we have 3hours to complete it..

if anyone can/will help, i'll post the c-code here @ ~12h00 GMT   (14h00 here in RSA , GMT+2)

i hav till 15h00 GMT to finish (i think)

plz then post back the asm code. it should be easy, its only segments of c-code..

Exsamples:

;*****************************************************************************
;* int fact(int n);                                                          *
;*****************************************************************************
%define n [ebp+8]
fact:
  push  ebp
  mov   ebp, esp
  mov   eax, 1
  cmp   dword n, 0                 ; if (n == 0)
  jz    .done                     ;   return 1;
                                   ; else {
  push  dword n
  dec   dword [esp]
  call  fact
  imul  eax, n                     ;   return fact(n-1)*n;
  add   esp, 4                     ; }
.done:
  mov   esp, ebp
  pop   ebp
  ret

;*****************************************************************************
;* int power(int x, int y);                                                  *
;*****************************************************************************
%define x [ebp+8]
%define y [ebp+12]
power:
  push  ebp
  mov   ebp, esp
  mov   eax, 1
  cmp   dword y, 0                 ; if (y == 0)
  jz    .done                     ;   return 1;
                                   ; else {
  push  dword y
  dec   dword [esp]
  push  dword x
  call  power
  add   esp, 8
  imul  eax, x                     ;   return power(x, y-1)*x;
                                   ; }
.done:
  mov   esp, ebp
  pop   ebp
  ret

;*****************************************************************************
;* int ackerman(int x, int y);                                               *
;*****************************************************************************
%define x [ebp+8]
%define y [ebp+12]
ackerman:
  push  ebp
  mov   ebp, esp
  
  mov   eax, y
  cmp   dword x, 0                 ; if (x == 0) {
  jnz   .elsif
  inc   eax                       ;   return y+1;
  jmp   .done                     ; }
  
.elsif:                           ; 
  cmp   eax, 0                     ; else if (y == 0) {
  jnz   .else
  push  1
  dec   dword x
  push  dword x
  call  ackerman                   ;   return ackerman(x-1, 1);
  add   esp, 8
  jmp   .done                     ; }

.else:                             ; else {
  dec   eax
  push  dword eax
  push  dword x
  call  ackerman                   ;   /* Calculate 2nd parameter: ackerman(x, y-1) */
  add   esp, 8
  push  eax
  dec   dword x
  push  dword x
  call  ackerman                   ;   return ackerman(x-1, ackerman(x, y-1));
  add   esp, 8                     

.done:                             ; }
  mov   esp, ebp
  pop   ebp
  ret

;*****************************************************************************
;* int binary_search(int n, int list[], int low, int high);                  *
;*****************************************************************************
%define high [ebp+20]
%define low  [ebp+16]
%define list [ebp+12]
%define n    [ebp+8]
binary_search:
  push  ebp
  mov   ebp, esp
  
  mov   eax, -1
  mov   ebx, low
  mov   ecx, high
  
  cmp   ebx, ecx                   ; if (low > high)
  jg    .done                     ;  return -1;
  
  mov   edi, list
  mov   esi, n
  lea   eax, [ebx+ecx]
  shr   eax, 1                     ; middle = (low+high)/2;
  
  cmp   esi, [edi+eax*4]           ; if (n == list[middle])
  je    .done                     ;   return middle;
  jg    .else                     ; if (n < list[middle])
  lea   ecx, [eax-1]               ;   high = middle-1;
  jmp   .params
.else:                             ; else
  lea   ebx, [eax+1]               ;   low = middle+1;
  
.params:
  push  ecx                       ; /* high */
  push  ebx                       ; /* low */
  push  edi                       ; /* list */
  push  esi                       ; /* n */
  call  binary_search             ; return binary_search(n, list, low, high);
  add   esp, 16

.done:
  mov   esp, ebp
  pop   ebp
  ret

global addf

;*****************************************************************************
;* void addf(float a, float b, float *x);                                    *
;*****************************************************************************
%define a [ebp+8]
%define b [ebp+12]
%define x [ebp+16]
addf:
  push  ebp
  mov   ebp, esp

  mov   eax, a                     ; Expand 'a' into a sign (BH),  
  shld  ebx, eax, 9               ;  exponent (BL) and fraction (EAX)
  and   ebx, 0x000001ff           ;  f1 = fraction of 'a', s1 = sign of 'a'
  and   eax, 0x007fffff           ;  e1 = exponent of 'a'
  jnz   .add0                     ; if ((fraction != 0) ||
  cmp   bl, 0                     ;     (unbiased_exponent != -127)) {
  je    .l0
  
.add0:
  or    eax, 0x00800000           ;   Add the J-bit
                                   ; }
.l0:
  mov   ecx, b                     ; Expand 'b' into a sign (DH),
  shld  edx, ecx, 9               ;  exponent (DL) and fraction (ECX)
  and   edx, 0x000001ff           ;  f2 = fraction of 'b', s2 = sign of 'b'
  and   ecx, 0x007fffff           ;  e2 = exponent of 'b'
  jnz   .add1                     ; if ((fraction != 0) ||
  cmp   dl, 0                     ;     (unbiased_exponent != -127)) {
  je    .while0
  
.add1:
  or    ecx, 0x00800000           ;   Add the J-bit
                                   ; }
.while0:
  cmp   bl, dl                     ; while (e1 != e2) {
  je    .l2
  jg    .while1                   ;   if (e1 < e2) {
  inc   bl                         ;     e1 = e1+1;
  shr   eax, 1                     ;     f1 = f1 >> 1;
  jmp   .while0                   ;   }
                                   ;   else {  
.while1:
  inc   dl                         ;     e2 = e2+1;
  shr   ecx, 1                     ;     f2 = f2 >> 1;
  jmp   .while0                   ;   }
                                   ; }
.l2:
  cmp   bh, 1                     ; if (s1 == 1) {
  jne   .l3
  neg   eax                       ;   f1 = -f1;

.l3:                               ; }
  cmp   dh, 1                     ; if (s2 == 1) {
  jne   .l4
  neg   ecx                       ;   f2 = -f2;
                                   ; }
.l4:
  add   eax, ecx                   ; f3 = f1+f2; (EAX now holds f3)
  cmp   eax, 0                     ; if (f3 < 0) {
  sets  bh                         ;   s3 = 1; 
  jg    .while2
  je    .done
  neg   eax                       ;   f3 = -f3;
                                   ; }
  
.while2:                           ; while ((f3 & 0xff000000) != 0) {
  test  eax, 0xff000000
  jz    .while3
  shr   eax, 1                     ;   f3 = f3 >> 1;
  inc   bl                         ;   e3 = e3+1;
  jmp   .while2                   ; }
  
.while3:                           ; while ((f3 & 0x00800000) != 0) {
  test  eax, 0x00800000
  jnz   .l6
  shl   eax, 1                     ;   f3 = f3 << 1;
  dec   bl                         ;   e3 = e3-1;
  jmp   .while3                   ; }
  
.l6:                               ; Combine s3, e3 and f3
  cmp   eax, 0
  jz    .done
  and   eax, 0x007fffff           ; Remove the J-bit
  ror   ebx, 9
  or    eax, ebx
  
.done:
  mov   edi, x
  stosd                           ; *x = a+b;
  
  mov   esp, ebp
  pop   ebp
  ret

Link to comment
Share on other sites

You didn't even post any C++.

The C-/c++ code is next to the asm code as comments..

The task was asked in another way, there was no c++ they asked, we had to create it by ourselfs.. :?

The paper with the Question was handed back in, but i'll get it and post it here for those who would like to try it out. :smile:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...