Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2005, 4:17 PM   #1
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
Question How do i convert this code into assembly ??

Im trying to find the GCD of two int,
below is an alog. that I tried to make it into assembly..
but it looks like im missing , can anyone help out ??

Gcd(int a, int b, int *result)
{ while(a!=b)
{
if(a > b)
a = a - b;
else
b = b - a;
}
*result = a;
}




void lab1( int a, int b, int *result )
{
__asm
{
mov esi, result
//save the current status of the registers
push eax
push ebx
push edx

//put the variables into the registers
mov eax, a
mov ebx, b


START_WHILE:
cmp eax, ebx
je END_WHILE

cmp eax,ebx
jle Else_Block

sub eax, ebx
jmp START_WHILE

Else_Block:
sub ebx, eax
jmp START_WHILE

END_WHILE:
move edx



//put the answer to result
mov [esi][0], edx

//restore the original status of registers.
pop edx
pop ebx
pop eax
}
return;

}
oxyi is offline   Reply With Quote
Old Jan 27th, 2005, 12:50 AM   #2
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
I didn't test this or your algorithm. The extra compare is unnecessary in your code.
; assumes eax contains 'a' variable and ecx contains 'b' variable
begin:  cmp eax, ecx
        je  getout
        ja  higher
        sub ecx, eax
        jmp begin
higher: sub eax, ecx
        jmp begin
getout: ; result returned in eax
This might be better.
; gcd - greatest common divisor
;       by Paul Hsieh
;
; input:
;   eax = a
;   edx = b
;
; output:
;   eax = gcd
;
; destroys:
;   edx
;   flags
;
gcd:    neg     eax
        je      L3
L1:     neg     eax
        xchg    eax,edx
L2:     sub     eax,edx
        jg      L2
        jne     L1
L3:     add     eax,edx
        jne     L4
        inc     eax
L4:     ret
__________________
-- lostcauz

Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
lostcauz is offline   Reply With Quote
Old Jan 27th, 2005, 11:14 AM   #3
Lance
Programmer
 
Lance's Avatar
 
Join Date: Oct 2004
Location: Chicago, IL USA
Posts: 74
Rep Power: 4 Lance is on a distinguished road
Send a message via AIM to Lance
$ gcc -S gcd.c
$ cat gcd.s

        .file   "gcd.c"
        .text
.globl Gcd
        .type   Gcd, @function
Gcd:
        pushl   %ebp
        movl    %esp, %ebp
        nop
.L2:
        movl    8(%ebp), %eax
        cmpl    12(%ebp), %eax
        jne     .L4
        jmp     .L3
.L4:
        movl    8(%ebp), %eax
        cmpl    12(%ebp), %eax
        jle     .L5
        movl    12(%ebp), %eax
        subl    %eax, 8(%ebp)
        jmp     .L2
.L5:
        movl    8(%ebp), %edx
        leal    12(%ebp), %eax
        subl    %edx, (%eax)
        jmp     .L2
.L3:
        movl    16(%ebp), %eax
        movl    8(%ebp), %edx
        movl    %edx, (%eax)
        popl    %ebp
        ret
        .size   Gcd, .-Gcd
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)"
__________________
/* LANCE */
C++;  /* this makes C bigger but returns the old value */
char *site = "slackwise.net",
     *home = "lance.slackwise.net",
     *pics = "flickr.com/photos/slackwise";
Lance is offline   Reply With Quote
Old Jan 31st, 2005, 1:53 PM   #4
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
wow.. thanks for all the replys ...

im just a beiginner in assembly in x86.

Still try to figure out why you guys did it in the format of L1: L2: .. etc
is that how everyone do it ?

and wow you guys's code are soooo complicated...
oxyi is offline   Reply With Quote
Old Jan 31st, 2005, 3:16 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
The GNU C compiler will convert your C code to assembly for you. I believe they used that to convert. Lance gives you an example of how to do it, if you have a *nix system.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 31st, 2005, 3:35 PM   #6
Lance
Programmer
 
Lance's Avatar
 
Join Date: Oct 2004
Location: Chicago, IL USA
Posts: 74
Rep Power: 4 Lance is on a distinguished road
Send a message via AIM to Lance
Indeed. GCC is definitely the best compiler in the world.

You don't even have to type out fullly accurate code to convert it. I simple copied and pasted his code into vim, saved it as gcd.c, then used -S to pre-process/convert to assembly. It's really nice, and fast!

Either way, it helps a lot to convert some C code to Assembly if you're trying to learn Assembly, or already know the basics, but want to see how a compiler does it. You eventually grow much respect for compiler coders, especially those of GCC. You can even compile Java to Assembly if you wish!

Again... GCC is the lord almighty. It is sad that DJGPP isn't as good as pure GCC, so Windows is such a crippling system for coders. :/ Unless you're doing those graphical applications in Visual Studio... then you're stuck with MS' compiler. Oh welll...

And sorry if I sound somewhat matter-of-fact-ly; I'm absolutely decaffeinated and sober. That should explain enough.
__________________
/* LANCE */
C++;  /* this makes C bigger but returns the old value */
char *site = "slackwise.net",
     *home = "lance.slackwise.net",
     *pics = "flickr.com/photos/slackwise";
Lance is offline   Reply With Quote
Old Feb 1st, 2005, 6:40 AM   #7
lostcauz
Hobbyist Programmer
 
Join Date: Nov 2004
Location: 1691 miles East of L.A.
Posts: 159
Rep Power: 4 lostcauz is on a distinguished road
I simply looked at your C code and wrote it in assembler. I offered some other code by a more experienced assembler programmer for comparison also. While compiler output is a good way to learn and see a conversion the goal, for me at least, is to write tighter, faster code than the compiler. Either way it's awesome to see some others interested in learning the language. I commented the code I wrote.
; assumes eax contains 'a' variable and ecx contains 'b' variable

begin:  cmp eax, ecx   ; compare the 2 variables
        je  getout     ; if equal, we are done
        ja  higher     ; if a is greater than b jump to higher
        sub ecx, eax   ; otherwise a<b so subtract a from b
        jmp begin      ; loop until equal
higher: sub eax, ecx   ; subtract b from a
        jmp begin      ; loop
getout:                ; result returned in eax
__________________
-- lostcauz

Stepped in what?...
Behind whose barn?...
I didn't even know they had a cow!
lostcauz is offline   Reply With Quote
Old Feb 1st, 2005, 3:21 PM   #8
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
Wow thanks a lot such a clear code
I thought my way was the only way to do this gcd problem

Yap I am in the process of learning assemlby, but my professor so far has
done a terrible job on teaching it, or you can say he is not teaching it,
we just got a sheet where it explain some loop control and if /else statement
then we are off on our own, and he is off in the silicon world...

I have programming experience in java and c++, i heard about the converting process, but the result is too weird to for me to understand it.
And im currently using Visual .Net to do my assemblly work...



now i have another question..

implement a function that counts the number of vowels in a given string


edi is input string's memory address.
i know i would use a for loop to loop through the string
and check each position for A E I O U and a e i o u ,

I kinda know the how the loop control work in assembly, but I am not sure
how do I check into the string for each of the vowel, as oppose in java, i can just use a for loop and check each position within the string till the end.
Is that how I suppose to do it in assembly as well ? and is string is the same thing as array ?

a simple java code would take care of this, but should i try to write it in java first and try to convert to assembly to see how is it done ?

Im sorry that I asked a lot questions, but i just want to know this stuffs, thanks a lot for any replys
oxyi is offline   Reply With Quote
Old Feb 1st, 2005, 6:24 PM   #9
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Probably not the best way, but this is how I'd do it:

findVowels:
    cmp [di], BYTE 'a'
    je increment
    cmp [di], BYTE 'e'
    je increment
    cmp [di], BYTE 'i'
    je increment
    cmp [di], BYTE 'o'
    je increment
    cmp [di], BYTE 'u'
    je increment
    cmp [di], BYTE 0
    je end
    inc di
    jmp findVowels
increment:
    inc di
    mov bx, counter
    inc WORD [bx]
    jmp findVowels

Oh, and remember, no problem with asking questions.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 1st, 2005, 7:45 PM   #10
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
so no for loop control is required in it ?

what is [di] ? and what does WORD[bx] do ?

hehe thanks
oxyi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC