Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-18-2007, 02:13 AM   #1 (permalink)
S.P.A.R.K.Y.
Recruit
 
S.P.A.R.K.Y.'s Avatar
 
Join Date: Oct 2007
Posts: 1
S.P.A.R.K.Y. is on a distinguished road
Exclamation PLZ HELP: translate c++ to asm

hey,

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

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:

Code:
;*****************************************************************************
;* 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
Code:
;*****************************************************************************
;* 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
Code:
;*****************************************************************************
;* 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
Code:
;*****************************************************************************
;* 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
Code:
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
S.P.A.R.K.Y. is offline   Reply With Quote
Old Today, 02:49 AM   #2 (permalink)
lyjg1203
Recruit
 
Join Date: Dec 2008
Posts: 13
lyjg1203 is on a distinguished road
Wink wow gold

wow goldcheap wow goldworld of warcraft gold buy wow gold world of warcraft gold
lyjg1203 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DU command adapted in C Deliverance Standard C, C++ 3 12-03-2006 09:56 PM
C Database Programming bakoo Platform/API C++ 3 11-20-2006 05:20 PM
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 02:16 PM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 03:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting