Page 3 of 3

Posted: Mon Nov 12, 2007 3:05 am
by Brendan
Hi,
Dkelly wrote:
Brendan wrote:something as simple as inserting a few instructions into existing code would involve searching for all CALL, JMP and branch instructions and adjusting the target addresses,
Actually it doesn't... (For the most part), and whomever can tell me why, I'll believe you've hand coded machine language :)
There's 2 types of references - references that use absolute addresses and references that use relative addresses.

If you insert bytes into the middle of a binary, then all absolute addresses where the target is after the insertion point need to be changed, and all relative addresses which cross the insertion point (e.g. a conditional branch that jumps over the insertion point) need to be changed.

There is 2 ways to avoid this that I can think of. The first way is to put a table of target addresses at the start of the binary, so that the table is always before any insertion point, and so that you only need to change that table itself rather than searching through code. This increases overhead because you end up using indirect addressing (e.g. "call [fixedAddress]" instead of "call targetAddress") and doesn't help much with instructions that use relative addresses (e.g. there is no "Jcc [fixedAddress]" instruction).

The other way is to use large amounts of padding so that you can replace the padding instead of inserting bytes. The only disadvantage here is the increased size (and related cache efficiency and disk bandwidth problems).


Cheers,

Brendan

Posted: Mon Nov 12, 2007 2:13 pm
by exkor
EDITED: I meant factorial here not sqrt, oh.

I'll give you usefull example why knowing hex can be important:

say you got SQRT function - calculates quadratic root. It is fact that loop that calculates sqrt with fixed point math will be faster until count(# of loop passes) reaches some critilal value - say 20 passes.

You have in your sqrt function something like ('number' is sqrt(number))

Code: Select all

cmp number, 20 
jle    fixed_math
fpu:
fixed_math:
on different CPUs with different MHz this '20' number will be different - you run you test program periodically changing '20' to see wthat is the best value for your target CPU and measuring the speed of the function.

You could use a register(if available) to store this 20 value but more efficient way would be to patch the code. You save your register and you only get execution penalty once when changing value.

Posted: Mon Nov 12, 2007 3:41 pm
by Dkelly
Brendan wrote: There's 2 types of references - references that use absolute addresses and references that use relative addresses.
Yeah if you're coding by hand you use relative addresses whenever possible, and you leave a few spare bytes after each ret. Seems like most jumps/branches are relative anyways. As last resort you can always jump out of the main program flow, execute however much you need, then jump back in. (a favorite trick of virus writers).

Dan K

Posted: Sun Nov 18, 2007 2:02 pm
by Red Shaya
Well, finally I'm not feeling like the craziest guy on earth because I'm challenging the idea of writing an OS from scratch (i.e. no assembler .. or editor ... or ANYTHING)

By the way, I'm seeing questions about how long does it take to write an OS. In my case it seems like a lifetime :-)

So here is the link to my wiki in case anyone else feels crazy enough.

http://bina-os.wikidot.com

Posted: Sun Nov 18, 2007 4:23 pm
by Dkelly
Red Shaya wrote: So here is the link to my wiki in case anyone else feels crazy enough.

http://bina-os.wikidot.com
Well, the end product sounds alot like the old <a href=http://oldcomputers.net/kim1.html>Kim-1 educational computer</a>. Cool idea to bring that up a little more modern. I'd bet your OS would be pretty valuable in educational environments. Generally now a days that sort of educational tool is probably best just simulated on a PC, but somehow doing it on real hardware makes it seem more exciting.

Good luck :)

Dan K

Posted: Mon Nov 19, 2007 2:26 pm
by crazygray
I think I'll try that. (Was doing bootloader but decided to do the whole thing)

Posted: Mon Nov 19, 2007 3:14 pm
by crazygray
Well,guys I have a basic bootloader COMPLETE!
(Applaude Now)In Hex by the way.