NASM BITS ALIGN 用法小结

一、NASM文档BITS和ALIGN介绍

(1) 6.1 BITS: Specifying Target Processor Mode

      The BITS directive specifies whether NASM should generate code designed to run on a processor operatingin 16−bit mode, 32−bit mode or 64−bit mode. The syntax is BITS XX, where XX is 16, 32 or 64.

      In most cases, you should not need to use BITS explicitly. The aout, coff, elf, macho, win32 andwin64 object formats, which are designed for use in 32−bit or 64−bit operating systems, all cause NASM toselect 32−bit or 64−bit mode, respectively, by default. The obj object format allows you to specify eachsegment you define as either USE16 or USE32, and NASM will set its operating mode accordingly, so theuse of the BITS directive is once again unnecessary.

       The most likely reason for using the BITS directive is to write 32−bit or 64−bit code in a flat binary file; this is because the bin output format defaults to 16−bit mode in anticipation of it being used most frequently to write DOS .COM programs, DOS .SYS device drivers and boot loader software.
       You do not need to specify BITS 32 merely in order to use 32−bit instructions in a 16−bit DOS program; if you do, the assembler will generate incorrect code because it will be writing code targeted at a 32−bit platform, to be run on a 16−bit one.
        When NASM is in BITS 16 mode, instructions which use 32−bit data are prefixed with an 0x66 byte, and thosereferring to 32−bit addresses have an 0x67 prefix. In BITS 32 mode, the reverse is true: 32−bit instructions require no prefixes, whereas instructions using 16−bit data need an 0x66 and those working on 16−bit addresses need an 0x67.
      When NASM is in BITS 64 mode, most instructions operate the same as they do for BITS 32 mode. However, there are 8 more general and SSE registers, and 16−bit addressing is no longer supported.
      The default address size is 64 bits; 32−bit addressing can be selected with the 0x67 prefix. The default operand size is still 32 bits, however, and the 0x66 prefix selects 16−bit operand size. The REX prefix is used both to select 64−bit operand size, and to access the new registers. NASM automatically inserts REX prefixes
when necessary.

      When the REX prefix is used, the processor does not know how to address the AH, BH, CH or DH (high 8−bitlegacy) registers. Instead, it is possible to access the the low 8−bits of the SP, BP SI and DI registers as SPL,BPL, SIL and DIL, respectively; but only when the REX prefix is used.

       The BITS directive has an exactly equivalent primitive form, [BITS 16], [BITS 32] and [BITS 64].The user−level form is a macro which has no function other than to call the primitive form.
       Note that the space is neccessary, e.g. BITS32 will not work!

(2)ALIGN

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值