First familiar with nasm [4] -- SECTION and labels

Source: Internet
Author: User

1. In nasm, the custom segments are aligned by 4 bytes by default.

The manual says, "It is implemented by adding the 'align 'qualifier after the segment definition line. For example, section. data align = 16 switches to the segment '. data' and specifies that it must be aligned to the 16-byte boundary ." I also added align 16 to the custom segment and found that the compiled code is still aligned with 4 bytes. Very strange.

2. Let's talk about labels.

Check the following code (which can run normally). The Code is written to mbr and loaded to 0x7c00.

----------------------------

Org 0 h

S: mov ax, s

Inc ax

.....

---------------------------

Q: How does the compiler handle label s? Is it translated into a real physical address or an offset address?

To answer this question, you only need to disassemble the compiled binary file and look at mov ax and s. The following is my disassembly result on bochs:

----------------------------------------------------------------------------

20177c00: (): mov ax, 0x0000; b80000

----------------------------------------------------------------------------

Mov ax and s are translated into mov ax, 0x0000. Obviously, when the compiler processes tags, it translates the tags into the corresponding "offset address"-Someone has to ask, but what you say about this "offset address" is unclear, what is the relative offset address? Is the offset relative to the first command in the source code? This must mention the org command. When the org operand is 0, this "offset address" is the first instruction relative to the program startup. If the org operand is a non-0 constant xx, this "offset address" requires the addition of xx. For ease of understanding, here is another example to change the above Code:

----------------------------

Org 10 h

S: mov ax, s

Inc ax

.....

---------------------------

Disassembly:

-----------------------------------------------------------------------------

0000: 7c00 (unk. ctxt): mov ax, 0x0010; b81000
----------------------------------------------------------------------------

Therefore, the tag is translated into an offset address ".

3, $ and $ are special labels: $ indicates the relative "offset address" of the first row of the current section, and $ indicates the relative "offset address" of the current row ". Let's take a small example and look at the Code:

-----------------------------

% Include "./rw_floppy.mac"
Org 7c00h
Entrance: read_floppy_side_o_sector_total_destsa_destea 0000, 7e00h; this is a macro defined by me and is responsible for loading the code of the second sector to 0 x: 0x7e00.
Jmp main_entrance
Times 510-($-$) db 0
Dw 0aa55h; the first sector is filled
Main_entrance:; the Code starts from the second sector.
[SECTION. test1]
Mov ax, $
Mov ax, $
Mov ax, $
Mov ax, $
[SECTION. test2]
Mov ax, $
Mov ax, $
Mov ax, $
Mov ax, $

----------------------------

The result of disassembly:

------------------------------

20177e00: (): mov ax, 0x7e00; b8007e
20177e03: (): mov ax, 0x7e03; b8037e
20177e06: (): mov ax, 0x7e00; b8007e
20177e09: (): mov ax, 0x7e00; b8007e
20177e0c: (): mov ax, 0x7e0c; b80c7e
20177e0f: (): mov ax, 0x7e0f; b80f7e
20177e12: (): mov ax, 0x7e0c; b80c7e
20177e15: (): mov ax, 0x7e0c; b80c7e

-----------------------------

Needless to say, the results are very clear.

4. A problem is found: nasm cannot perform the label subtraction operation across segments. Check the Code:

---------------------------

Org 7c00h
Entrance: read_floppy_side_o_sector_total_destsa_destea, 7e00h
Jmp main_entrance
Times 510-($-$) db 0
Dw 0aa55h
Main_entrance:
[SECTION. test1]
S1: db 0
[SECTION. test2]
S2: db 0
S3: mov ax, s3-s1; note that s1 and s3 are labels for different sections
--------------------------

If the code above cannot be compiled, an error is returned: invalid operand type.

However, the following code can be compiled:

---------------------------

Org 7c00h
Entrance: read_floppy_side_o_sector_total_destsa_destea, 7e00h
Jmp main_entrance
Times 510-($-$) db 0
Dw 0aa55h
Main_entrance:
[SECTION. test1]
S1: db 0
[SECTION. test2]
S2: db 0
S3: mov ax, s3-s2; note that s2 and s3 are labels of the same SECTION
--------------------------

I guess nasm prohibits the division of tags across segments. I don't know if I have any friends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.