Upcoming Posts

Upcoming Posts....

Make your own partition magic software.
How to make an assembler.
What is booting?
Write a simple OS!

‎"I have no special talents. I am only passionately curious." - Albert Einstein

Sunday, January 1, 2012

What is ORG (origin) directive in assembly level language?

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.

12 comments:

  1. Thank You sir. Why are you not continuing your blog these days. Hoping to see some more useful posts in future.

    ReplyDelete
    Replies
    1. Thanks for encouraging. I was a bit busy with some other projects and will start posting soon...

      Delete
  2. This 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?

    ReplyDelete
    Replies
    1. Yes, the size of its previous instruction in machine language is 2 (It's not about AX and BX)

      Delete
  3. hi any one suggest me that

    am using org 300h
    in the program,, but it will not start at the same address why? ....it start some adress like 30Eh....

    ReplyDelete
    Replies
    1. ORG just sets the location counter required during compilation. Loading binary and execution is different thing.

      Delete
  4. hi..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..?

    ReplyDelete
    Replies
    1. It's very easy to develop an emulator. I had done it too during graduation. You need to know instructions, memory etc details.

      Delete
  5. WHAT IS THE PROCESSING SPEED OF 8085 MICROPROCESSOR ? HOW CAN WE KNOW PRACTICALLY BY SIMULATOR ?

    ReplyDelete
  6. sir, i did write program with org 0x10000000, but i want load and execute that program from memory address 0x30000000. How can i do this.

    ReplyDelete
  7. sir, i did write program with org 0x10000000, but i want load and execute that program from memory address 0x30000000. How can i do this.

    ReplyDelete
  8. t 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