![]() |
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; } :
|
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:
; gcd - greatest common divisor |
$ gcc -S gcd.c
$ cat gcd.s :
.file "gcd.c" |
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: |
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.
|
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 |
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 |
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 :) |
Probably not the best way, but this is how I'd do it:
:
findVowels:Oh, and remember, no problem with asking questions. :) |
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