nasm報錯:operator may only be applied to scalar values

來源:互聯網
上載者:User

  搜到了一篇不錯的文章:

  (回答的那個人實在是太敬業了)

Hey all.

I'm a complete beginner in assembly and have chosen NASM to work my way up with. But I have a problem with the pseudo-instruction set. More precisely, I don't understand how the expressions "$ and $$" are used in correlation with times.

It's no problem for me to understand what "times" does, but I do not understand the expressions used together with times.

Like;

times 512-($-$$) db 0

It is explained what it does (in the tutorial), but not how it is done.

My questions according to this example;
1. Why is there a dash ("-") after 512 and before "(" ?
2. Can someone elaborate what $ means and is used for?
3. Can someone elaborate waht $$ means and is used for?

I read the 2 lines of description about $ and $$ multiple times now, and I still don't understand what they are. Can someone help?

下面是回答:
The mysterious '-' is a minus sign. That takes care of that part.

Nasm uses '$' for several purposes, in this case it's "here" - the current assembly location. Actually, the location at the beginning of the line.

'$$' is the beginning of the current section.

You might reasonably think that '$' would be enough, but '$', like any symbol, is a "relocatable value". That is, the linker and loader can alter this value in ways Nasm knows nothing about. In the specific case of "-f bin" output, Nasm acts as its own linker,
but that happens in the "output driver", and we need to know this value in the actual "assemble" routine, which runs first. This is what Nasm means when it says "not a scalar value". It's a vector??? No, it's a "relocatable value".

In some cases, a relocatable value is okay...

Code: [Select]
mov eax, $

Nasm doesn't need to know the value at "assemble-time", it codes "mov eax, (this label)", and the linker/loader will fill in the relocated value. But:

Code: [Select]
mov eax, $ << 1

... now we're asking Nasm to do an assemble-time calculation on a value it doesn't know, so it whines.

The difference between two labels (in the same section!) is a "scalar value". The linker/loader can change the two values, but the distance between them remains the same. Therefore, we can do a calculation on it - and it's an acceptable argument to "times".

Code: [Select]
org 0 ; the default, if no origin specifiedsection .text ; the default, if no section specifiednopnopnop ; three bytes of "code"times 512 - ($ - $$) db 0

In this case, '$' is 3 and '$$' is 0, so we're emitting 509 zeros to bring the total file size to 512 bytes. In this case, just '$' would have worked, if Nasm would accept it, which it won't.

Code: [Select]
org 7C00hsection .textnopnopnop ; three bytes of "code"times 512 - ($ - $$) db 0

Now, '$' is 7C03h and '$$' is 7C00h. We're still adding 509 zeros to pad to 512 bytes, but in this case '$' by itself would *not* do what you want!

The expression "512 - ($ - $$)" can be written without the parentheses as "512 + $$ - $", but this is even less clear (IMHO). In a bootsector, 510 is more common than 512, since we want the two byte "boot signature" after it, and the entire bootsector needs
to be 512 bytes, but that doesn't alter '$' and '$$'...

Does that make you less confused, or more?

Best,
Frank

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.