汇编语言中的ORG指令

本文详细解释了ORG指令在汇编语言中的作用,作为伪指令用于规定程序的起始地址,以及在51单片机、X86DOS和Windows32EXE中的差异。它在编译过程中的链接阶段发挥作用,特别关注了在51单片机中如何通过ORG确保程序正确链接到HEX文件和ROM的地址。
摘要由CSDN通过智能技术生成

一 ORG指令

ORG是Origin的缩写:起始地址,源。在汇编语言源程序的开始通常都用一条ORG伪指令来实现规定程序的起始地址。如果不用ORG规定则汇编得到的目标程序将从0000H开始。例如:

            ORG 2000H   
   START:MOV  AX,#00H

汇编语言源程序中若没有ORG伪指令,则程序执行时,指令代码被放到自由内存空间的CS:0处;若有ORG伪指令,编译器则把其后的指令代码放到ORG伪指令指定的偏移地址。两个ORG伪指令之间,除了指令代码,若有自由空间,则用0填充。

预处理–>编译–>汇编–>链接,这是高级语言的编译全过程。对于纯汇编,就只有汇编和链接两个步骤。

org指令是链接时使用的,不是汇编那一步使用的。即不是cpu的一条指令,而是给编译器看的伪指令。

在51单片机、x86 dos、win32 exe中实现都有差异。

以最简单的51单片机为例,编译器最终链接生成HEX文件,再烧录到51的rom中去。单片机很简单,没有x86的分段、分页,启动就是从rom的0x0的位置开始执行。同时0x30H位置开始存放中断向量,
所以单片机程序一般开头就是:

   ORG 0000H


   AJMP MAIN


   ORG 0030H

这里的ORG 0000H指令,使得程序链接成HEX文件时,AJMP MAIN这条指令的机器码就在HEX文件的0x0位置。这是链接器的工作。

二 英文版

ORG用来自定义程序的起始位置,从而达到control section和 REDFEIN portions的目的

If a control section has not been previously established, ORG initiates an unnamed (private) control section.

>>-+--------+--ORG---------------------------------------------->
   '-symbol-'        

>--+--------------------------------------------+--------------><
   '-expression-+-----------------------------+-'   
                '-,-+-boundary--+---------+-+-'     
                    |           '-,offset-' |       
                    '-,--offset-------------'       

这玩意内部可以分成如上图所示
symbol
Is one of the following:

  • An ordinary symbol
  • A variable symbol that has been assigned a character string with a value
    that is valid for an ordinary symbol
  • A sequence symbol。

If symbol denotes an ordinary symbol, the ordinary symbol is defined with the value that the location counter had before the ORG statement is processed.

expression
Is a relocatable expression, the value of which is used to set the location counter. If expression is omitted, the location counter is set to the next available location for the current control section.
boundary
Is an absolute expression that must be a number that is a power of 2 with a range from 2 (halfword) to 4096 (page). If boundary exceeds the SECTALGN value, message ASMA500E is issued. This message is not issued if the section being processed is a Reference Control Section (DSECT, DXD, or COM).
boundary must be a predefined absolute expression whose value is known at the time the ORG statement is processed.

If the boundary operand is greater than 16, the GOFF option must be specified in addition to the SECTALGN option.

offset
Any absolute expression
If boundary or offset are provided, then the resultant location counter is calculated by rounding the expression up to the next higher boundary and then adding the offset value.

origin   csect
         ds    235x               Define 235 bytes
         org   origin,,3          Move location counter back to start + 3
         org   *,8                Align on 8 byte boundary
         org   *,8,-2             Align to 8 byte boundary -2 bytes
translate dc   cl256' '           Define aligned translate table
         org   translate+c'a'
         dc    c'ABCDEFGHI'
         org   translate+c'j'
         dc    c'JKLMNOPQR'
         org   translate+c's'
         dc    c'STUVWXYZ'
         org   translate+c'A'
         dc    c'ABCDEFGHI'
         org   translate+c'J'
         dc    c'JKLMNOPQR'
         org   translate+c'S'
         dc    c'STUVWXYZ'
         org   ,
         end

下面是上面那个程序的解析

                  Source Module                      │      Object Code
─────────────────────────────────────────────────────┼────────────────────────
                                                     │
          FIRST    START 0..1        TABLE    DC    XL256'0'                    │ TABLE   (in Hex)
 2                 ORG   TABLE+0                     │ +0       ┌────┐
        ┌          DC    C'0'        3               │          │ F0 │
        │          DC    C'1'                        │          │ F1 │
        │          .                                 │          │ .  │
        │          .                                 │          │ .  │
        │          ORG   TABLE+13                    │ +13      │ .  │
        │          DC    C'D'                        │          │ C4 │
        │          DC    C'E'                        │          │ C5 │
        │          .                                 │          │ .  │
        │          .                                 │          │ .4     ─┤          ORG   TABLE+C'D'                  │          │ .  │
        │          DC    AL1(13)                     │ +196     │ 13 │
        │          DC    AL1(14)                     │          │ 14 │
        │          .                                 │          │ .  │
        │          .                                 │          │ .  │
        │          ORG   TABLE+C'0'                  │ +240     │ .  │
        │          DC    AL1(0)                      │          │ 00 │
        │          DC    AL1(1)                      │          │ 01 │
        │          .                                 │          │    │
        └          .                                 │ +255     └────┘
                   ORG                               │
 5        GOON     DS    0H                          │
∧                  .                                 │
TABLE+256          .                                 │
                   TR    INPUT,TABLE                 │
                   ..                                 │
          INPUT    DS    CL20                        │
                   ..                                 │
                   END                               │
  • 14
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值