How the assembler works(ARM)

本文深入探讨了ARM汇编器的工作原理,重点解释了两阶段组装流程,包括第一阶段检查语法、确定指令大小和定位标签位置等操作,以及第二阶段编码指令、生成重定位和调试信息的过程。同时,通过示例展示了如何避免在组装过程中出现错误。

The ARM assembler is a 2 pass assembler that outputs object code from the assembly language source code. This means that it reads the source code twice. Each read of the source code is called a pass.

This is because assembly language source code often contains forward references. A forward reference occurs when a label is used as an operand, for example as a branch target, earlier in the code than the definition of the label.The assembler cannot know the address of the forward reference label until it reads the definition of the label. During each pass, the assembler performs different functions.

During the first pass, the assembler:

  • Checks the syntax of the instruction or directive. It faults if there is an error in the syntax, for example if a label is specified on a directive that does not accept one.

  • Determines the size of the instruction and data being assembled and reserves space.

  • Determines offset of labels within sections.

  • Creates a symbol table containing label definitions and their memory addresses.

During the second pass, the assembler:

  • Faults if an undefined reference is specified in an instruction operand or directive.

  • Encodes the instructions using the label offsets from pass 1, where applicable.

  • Generates relocations.

  • Generates debug information if requested.

  • Outputs the object file.

Memory addresses of labels are determined and finalized in the first pass. Therefore, the assembly code must not change during the second pass. All instructions must be seen in both passes. Therefore you must not define a symbol after a :DEF: test for the symbol. The assembler faults if it sees code in pass 2 that was not seen in pass 1. Example 1 shows that num EQU 42 is not seen in pass 1 but is seen in pass 2.

Example 1. Line not seen in pass 1

    AREA x,CODE 
    [ :DEF: foo 
num EQU 42 
    ] 
foo DCD num 
    END

Assembling the code in Example 1 generates the error:

A1903E: Line not seen in first pass; cannot be assembled.

Example 2 shows that MOV r1,r2 is seen in pass 1 but not in pass 2.

Example 2. Line not seen in pass 2

    AREA x,CODE 
    [ :LNOT: :DEF: foo 
    MOV r1, r2 
    ] 
foo MOV r3, r4
    END

Assembling the code in Example 2 generates the error:

A1909E: Line not seen in second pass; cannot be assembled.
Copyright © 2010-2011 ARM. All rights reserved.ARM DUI 0473C
Non-ConfidentialID080411

转载于:https://my.oschina.net/u/218425/blog/76916

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值