The origin directive tells the assembler where to load instructions and data into memory. It changes the program counter to the value specified by the expression in the operand field. Subsequent statements are assembled into memory locations starting with the new program/location counter value. If no ORG directive is encountered in a source program, the program counter is initialized to zero.
Assembler uses an internal variable called LC (Location Counter) to store current offset address of the statement being processed. When it encounters a variable declaration statement, it puts the value of LC in its symbol table as variable’s address.
For example:
; Initial value of LC is 0
MOV AX, BX ; Here LC = 0
MOV CX, DX ; Now LC = LC + size of above statement i.e. LC = 0 + 2 = 2
A db 0; ; LC = LC + size of above statement i.e. LC = 2 + 2 = 4.
; So the address of “A” will be 4 as LC = 4 when variable definition appear.
MOV DX, A ; LC = LC + size of above statement i.e. LC = 4 + 1 = 5
; In above statement “A” will be replaced with the address of “A” which is 4.
; At end LC = 5 + 4 = 9
This program will work when it is loaded at offset 0 in the segment pointed by DS register. I.e. loading this program at 200:00h (Segment : Offset) or 700:00h address will work as the offset address is 00h.
What if we need to load this program at 200:300h address? Here DS = 200h and offset = 300h (offset != 0), the variable “A” is physically located at “200:304h” address. But the program will try reading its value from 200:04h address. It is obvious that we will not get expected result as the program is not reading variable from its actual address (200:304h).
This program would have worked if the initial value of LC was 300h. Isn’t it?
So we need a directive which can instruct assembler to initialize LC with a specific value like 300h. The directive “ORG” does this. In such scenarios, we would need to use “ORG XXh” statement at the begging of the program to initialize LC with value XXh.
The bottom line is that we should use “ORG” directive when DS (Data Segment) register is not pointing to the first variable in Data segment (when program has separate Code and Data segment) or first instruction (when program has only one segment for both Code and Data).
This directive is very useful when writing boot loader, device drivers, virus, antivirus and OS components because these programs need to loaded at particular offset address.
Thank You sir. Why are you not continuing your blog these days. Hoping to see some more useful posts in future.
ReplyDeleteThanks for encouraging. I was a bit busy with some other projects and will start posting soon...
DeleteThis really helped to understand ORG but can u explain, in your sample code, how come LC is 0+2? As in where did this 2 come from? Are you counting AX and BX as 2 together? Can you explain your example?
ReplyDeleteYes, the size of its previous instruction in machine language is 2 (It's not about AX and BX)
Deletehi any one suggest me that
ReplyDeleteam using org 300h
in the program,, but it will not start at the same address why? ....it start some adress like 30Eh....
ORG just sets the location counter required during compilation. Loading binary and execution is different thing.
Deletehi..i have worked on an 8085 simulator during my intern at iitg and now i m working on an ARM simulator and want to develop my own..so,what is the prerequired knowledge that i should have..?
ReplyDeleteIt's very easy to develop an emulator. I had done it too during graduation. You need to know instructions, memory etc details.
DeleteWHAT IS THE PROCESSING SPEED OF 8085 MICROPROCESSOR ? HOW CAN WE KNOW PRACTICALLY BY SIMULATOR ?
ReplyDeletesir, i did write program with org 0x10000000, but i want load and execute that program from memory address 0x30000000. How can i do this.
ReplyDeletesir, i did write program with org 0x10000000, but i want load and execute that program from memory address 0x30000000. How can i do this.
ReplyDeletet provides control over memory layout, crucial for efficient program execution and data organization. Having Games Bad By specifying the starting address, programmers optimize memory utilization and ensure proper program functioning.
ReplyDelete