0
votes

If my question sound nonsensical, pardon me.

But I am quite confused, say I am defining the constant buffer_size there is a line on the code that I am studying which says: buffer_size equ 16, which in my mind means, make buffer_size 16 big. But in other code samples that I review, numbers do have the character h beside them, which I'm told is to tell the assembler to treat the number as hexadecimal.

If a number doesn't have the h beside it, does it make it decimal then?

2

2 Answers

3
votes

Yes, MASM (and pretty much all other modern assemblers1) are like C/C++: numeric literals are decimal by default.

You can use other bases with suffixes. See How to represent hex value such as FFFFFFBB in x86 inline assembly programming? for the syntax. Some assemblers, like NASM, allow 0x123 as well as 123h, but MASM only allows suffixes.

10h in MASM is exactly like 0x10 in C, and exactly equivalent to 16.

The machine code assembled doesn't depend on the source representation of the number. (mov eax, 10h is 5 bytes: opcode and then 32-bit little-endian binary number, same as mov eax, 16.)

The same goes for foo: db 0FFh: code that adds something to it isn't "adding hex numbers", it's just a normal binary number. (A common beginner mistake (in terminology or understanding, it's usually not clear which) is to confuse the source-code representation with what the machine is doing when it runs the assembler output.


Footnote 1: Ancient assemblers might be different. There might be a few assemblers for some non-x86 platforms that also don't default to decimal.

The one built-in to the obsolete DOS DEBUG.EXE treats all numeric literals as hex, so mov ax, 10 = mov ax, 8+8. (If it even evaluates constant expressions, but if not then you know what I mean.)

DEBUG.EXE doesn't even support labels, so its basically horrible by modern standards; don't use it. These days there are free open-source assemblers like NASM, and also debuggers including at least the one built-in to BOCHS, so there's no need to suffer with old tools.

Anyway, this sidetrack about DEBUG.EXE isn't really relevant to your question about MASM; I only mention it as the only example I know of of an assembler that doesn't default to decimal. They do exist, but it's not normal these days.

3
votes

Be careful, understand that assembly languages are generally not standardized in the way that many higher level languages are, so the question is quite vague, you didnt even state the instruction set. The tag masm32 implied x86 (that tag was added for you).

It seems that you wanted x86 and the specific subset of the masm family of assemblers.

Assembly is generally defined by the assembler, the tool, not the instruction set. So when wanting to know how an assembly language works or its rules you have to look at the assembler itself. Its documentation if any or if good enough, if not you have to experiment.

I dont have masm32 handy, requires some pain to get it, but I have another readily available assembler and you could experimentally answer your own question. (as pointed out already in another answer, yes without the h in masm it defaults to decimal)

mov al,10h
mov al,0x10
mov al,10

which disassembles to

00000000  B010              mov al,0x10
00000002  B010              mov al,0x10
00000004  B00A              mov al,0xa

In this case not specified means defaults to decimal, which is what you should expect at least for instructions from masm.

Non-instruction syntax which is also part of the assembly language may have different syntax rules than the instruction part of the language. One would hope a tool uses the same rules for numbers throughout, but you never know.

Likewise there may be instructions that use an immediate as an offset to a register rather than a value being loaded into a register, one would hope those immediates/values also follow the same rules.

Best to experiment and be sure rather than hope that a manual or a web page is complete and correct.

To your title question, which is again very vague, yes there are assemblers out there that understand octal, decimal and hexadecimal (and maybe other bases like base 2) not necessarily all within one tool, and not limited to x86 since the title question did not. And what they default to and what syntax is required to specify a base is specific to each tool. Point being assembly language is not like other programming languages, cannot make generalizations about assembly language. Would be simple for someone to create a new assembler for some target that doesnt conform to the generalization, yet be a perfectly usable tool.