Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   How do i convert this code into assembly ?? (http://www.programmingforums.org/showthread.php?t=1990)

oxyi Jan 26th, 2005 4:17 PM

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;

}


lostcauz Jan 27th, 2005 12:50 AM

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


Lance Jan 27th, 2005 11:14 AM

$ 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)"


oxyi Jan 31st, 2005 1:53 PM

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... :confused: :confused:

Mjordan2nd Jan 31st, 2005 3:16 PM

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.

Lance Jan 31st, 2005 3:35 PM

Indeed. GCC is definitely the best compiler in the world. :P

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! :D

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. :P

lostcauz Feb 1st, 2005 6:40 AM

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


oxyi Feb 1st, 2005 3:21 PM

Wow thanks a lot such a clear code :)
I thought my way was the only way to do this gcd problem :D

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 :)

Mjordan2nd Feb 1st, 2005 6:24 PM

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. :)

oxyi Feb 1st, 2005 7:45 PM

so no for loop control is required in it ?

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

hehe thanks


All times are GMT -5. The time now is 4:28 PM.

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