Download tcj_27 1986-11
Transcript
LD HL,FllelO INC (HLl (HLl JP Address of general fi Ie 1/0. Change INC into DEC for reading. Execute and return through it. , While this type of code would be difficult to manage because it would have to remember the last state, it could be made to function. Relocatable Code The z-ao does not process relocatable code. So the program must handle the relocation task itself before it is executed. While there are many ways to do this, the most common procedure to to maintain a table of those address that have to be relocated. TURBO Pascal, in the overlay command processor (TURBO.OVR) uses this technique. The program is assembled as if it begins at location zero (or some such address). Just prior to being executed, a relocation routine moves it to the destination address (in the case of TURBO.OVR this is high memory) and then changes all address references accordingly. Finally the program is executed. A second procedure is to maintain a bit map table within the program (some assemblers help you maintain such a table). This table has a bit for every byte in the program. If the bit is set, the corresponding byte must be relocated. This works for programs which are relocated by a certain number of pages (256 bytes to a page). That way only the high byte of an address must be changed. Trick Code You may find that assembly programmers love to use certain "tricks" that improve execution speed or save memory. You have to watch out for this or you could get really confused. TURBO Pascal in many places uses instruction sequences like: ReadF i Ie: LD ORG WritaFi le:XOR AND PUSH A,OAFH $-1 A A AF By jumping into the middle of the instruction at "ReadFile" the program is able to set a flag byte (using register A) that is zero on a write and non-zero on a read. This saves a few bytes and drives the disassembler crazy! Change these into something like: ReadFi Ie: DB WriteFile:XOR AND PUSH 3EH A A AF And add some good comments so you will know what is going on the next time you have to look at this. One way of logging an error message where it is desired to load register A with a value (say from 1 to 5) to correspond to an error code, could be done as follows: Error5: INC A Error4: INC A Error3: INC A Error2: INC A Error1: INC A LD (ErrorCode),A To log an error then, the following sequence would'work (this saves a single byte for each occurrence). 12 XOR A CALL Error4 Initially when you disassemble this type of code sequence, you may be tempted to think that the series of "INC A" instructions are really data. After all it doesn't make much sense at first. But as you uncover those locations where these instructions are referenced, you soon discover what is going on and correct your mistake. There are plenty of other tricks you may run into and some are even more obscure. Making Use of the Generated Source Code Now that you have generated the source code and it has been documented more of less to your liking, what do you do with it? Although you probably should have asked yourself this before you started this project, here is a list of the major uses for the source code to a program. • Using the source code you can fIX those "bugs" that prevented you from fully utilizing the product. • Set those default parameters to what you want. • Modify the program to include features that you (and perhaps others) want to see. • Learn how the programmer did those wonderful things. Make your programs work just as well using these techniques. • Make some money from your labor. Because you have converted the program from machine code to an ASCII file suitable for an assembler, you can now edit the file making any changes you wish. Then you reassemble and link (or load) the program. This new copy now functions the way you want it to. If one of your reasons for producing the source code was to learn how the program functioned, then the printed listing is probably the most important result. While the process of disassembling already gave you great insight into the program as a whole, now you will be able to study that particular aspect that intrigued you (like just how does TURBO Pascal handle a forward label reference when it is only a one pass compiler? Is it really a one pass compiler??). Another interesting, but often overlooked, use for the source code is that other people may have similar interests to yours. Maybe you could sell it? This is a sticky area because the source code is composed of instructions whose order (ie, the executable part of the program) is owned by the author, plus your added comments and label names. There have been a few examples of disassembled programs being published (for example the Timex/Sinclair ROM) but these most likely required the consent of the original author. Regarding your work, you would certainly be allowed to sell your label names and comments but probably not the instructions. After all if you sold the instructions then you are, in effect, selling the program and would owe the author royalties. But all is not lost as there is a way out of this mess. If you consider that every line of the assembly source file looks like this: <---------- -------> <-- " ---> 12 <---------- 13 -------> <--- 14---> (LabeIName) (processor instruction) (instruction "rguments) (Comments) where each field could be absent, then you could extract the portions that you have created (*1 and 1/4) from the line and eliminate the portions that the program author created (1/2 and 1/3), The extracted portions could be put into data files and sold. Now in order for a customer to use this data (your label names The Computer Journal/Issue 1127