Page 1 of 1
Help me convert C to assembly!
Posted: Mon Jan 04, 2010 8:45 pm
by earlz
I've acquired a snipplet of C code and I am having some extreme difficulties figuring out what is wrong with my assembly translation of it.
The C code:
Code: Select all
//NUM=320*200 (yes I know it will basically never finish)
do {
for (i = 0; ; i++){ //note, this compiles and works. I've simplified it as much as possible to make it simple to convert to assembly
if(i>=NUM){
break;
}
values[i]++;
if(values[i]!=0){
break;
}
print_val();
}
} while (i < NUM);
Well, whenever I use this code in assembly, it does not do what is expected. It very quickly "finishes" yet the data set doesn't appear to be changed(though I can't check it thoroughly)
Code: Select all
main_loop:
;ds:0 is start of `values` array
mov bx,0 ;i=0
.inner:; for(..) {
cmp bx,320*200
jge .exit_loop ;i>=320*200 then break
inc byte [bx] ;values[i]++
cmp byte [bx],0
jne .exit_loop ;values[i]!=0 then break
inc bx ;i++
;inc bx
jmp .inner ;}
.exit_loop:
cmp bx,320*200
jl main_loop ;i<num
.exit:
Also, in both versions the `values` array has all elements zeroed.
Re: Help me convert C to assembly!
Posted: Mon Jan 04, 2010 9:27 pm
by geppyfx
jge & jl are for signed comparison and therefore your bx becomes limited to 15bits=32768 values
try using jae & jb
Re: Help me convert C to assembly!
Posted: Fri Feb 05, 2010 8:05 pm
by DednDave
I take it you realize it is for a 16-bit program.
If you want it to be for 32-bit code, use EBX for addressing.
EDIT - also, it will exit on the first INC byte ptr [BX] where [BX] is not 0FFh.
maybe you meant...
Re: Help me convert C to assembly!
Posted: Sat Feb 06, 2010 9:24 am
by Coty
@Dex: Anyone should have a chance to learn assembly, after all, it is one of the most grand
languages
And the more who know it, the eisyer to find it stuff than just C
Re: Help me convert C to assembly!
Posted: Sat Feb 06, 2010 5:50 pm
by pcmattman
Dex wrote:This looks like a case of cutting and pasting some code snip you found, when you do not understand how it works.
I thank you Dex for giving me a really good laugh this morning. It's just what I needed.
I find it funny that you find it perfectly fine to
provide such code snippets but somehow when someone copy and pastes a snippet it's a bad thing?
Right, I'll agree with you when you stop posting code. I think that's fair.
Re: Help me convert C to assembly!
Posted: Sat Feb 06, 2010 8:39 pm
by DednDave
it's rather obvious it wasn't copy/pasted - it doesn't work
Earlz
One thing you can do is compile the C code and let the compiler spit out an assembler listing.
But, I'd be really surprised if it is 16-bit code.
Although 16-bit may be required for writing boot-loaders, it isn't the "normal" mode.
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 4:44 am
by neato
I like Dex, I don't mind him pointing to code or posting snippets, I don't care if they are commented either. The seeker should already know enough about the driver he/she needs to write that it wouldn't matter if they receive code without comments or even code written in a different programming language than the one you are writing in. I've mastered just enough ASM to maybe write Hello World to the screen, but if I am shown the code and I know the specs, it all looks like C to me. Now if there is a part of the code that just knocks me for a loop, then its my option as the seeker to return to my thread and ask for clarification. I think Dex would provide that.
As for what he said above, I thought it was hilarious. He seemed mad as hell.
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 7:03 am
by Coty
pcmattman wrote:Dex wrote:This looks like a case of cutting and pasting some code snip you found, when you do not understand how it works.
I find it funny that you find it perfectly fine to provide such code snippets but somehow when someone copy and pastes a snippet it's a bad thing?
all part of Dex's theory that I have seen, code should be provided as example, and to toy with to learn
from. but should not just be 'copy & pasted', but seeing how the code works and having
the chance to take it apart and see all the geers and how the move.
Also as a plus:
Dex wrote:Thats the way i like to work, write my own code, but i like to have working code, just in case i get a bug, that i can not work out, i can see where the working code is differant.
I think this is the best way to fix bugs, a good
CODE example can show how it works, and give
atleast one idea how to fix the problem.
-------------
You know how I learned ASM? Dex gave me a link to minidos and a good asm tut, I took minidos
apart and learned how the mov, jmp, call, ect instructions worked. Between the two I learned
Real mode, when I had something I didn't understand, minidos had the answer
Also, read this post.
http://board.flatassembler.net/topic.ph ... 808#103808
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 9:07 am
by cxzuk
I dont understand what this is meant to do?
values++;
if (values!=0) {
break;
}
This condition will "always" be true, and so it will break out of the for loop, hit the while, then reenter the for loop resetting i to 0.
Unless you "values[]" are negative numbers (which you will have to consider in you ASM), your "values[]" are overflowing and looping back round to 0.
Code: Select all
for (i = 0; i >= NUM; i++) {
do { values[i]++; } while(values[i] != 0);
print_val();
}
should be the same result, but easier to convert to asm.
Tho, I would seriously advise not doing so. I really do think you should try learning abit more about programming, why not try an easier language like PHP. simple to debug, very forgiving on your coding, and has a wealth of documentation and example code.
MikeyB
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 9:21 am
by neato
Oh, no you didn't... PHP? That's rough. Anyway, it's too much like C, maybe try HTML first. You gotta crawl before you can walk. j/k earlz
Seriously though, he must be able to code, I mean how could someone who has 1476 posts and has been here since 2005, not know how to program?!?! It's simply not possible, is it?
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 9:36 am
by cxzuk
neato wrote:Oh, no you didn't... PHP? That's rough. Anyway, it's too much like C, maybe try HTML first. You gotta crawl before you can walk. j/k earlz
Seriously though, he must be able to code, I mean how could someone who has 1476 posts and has been here since 2005, not know how to program?!?! It's simply not possible, is it?
I have just read
http://forum.osdev.org/viewtopic.php?f= ... ex#p168525
If his other 1475 posts are like that, then im surprised hes not been kicked out and banned. Not only was his reply way off the original question (which was a good question too). But his reply was extremely rude.
What an @$$
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 10:05 am
by neato
You're taking it kind of personal, what's it mean to you if someone said something rude to Dex? Are you like his brother or something?
Re: Help me convert C to assembly!
Posted: Sun Feb 07, 2010 1:34 pm
by neon
Hello,
I would not consider this copying and pasting. He is building the C code as assembly; that is not the same as taking code, pasting it, and expecting it to work.
That C code can be made easier. ie, it can be made with a single for loop with the if(i>=NUM) test case and outer loop omitted. Simplifying the C code might help make the assembly language version easier to write and code easier to understand.