2

In my searching although this question seems to get answered a lot, the answers don't really cover what I want to ask.

I'm learning assembly and my question is, in higher level languages e.g C data types are defined as a certain length e.g

Char: 1 byte

Short: 2 bytes

Long: 4 bytes

Here's a block of memory, say I defined a variable of type Short in my high level language and it just so happenes to start at memory location 0x0002.

0x0001 00

0x0002 FF

0x0003 FF

0x0004 00

How does the generated assembly know what type it is and to only read 2 bytes. Is it the case that the compiler scans the code for any overflow errors when compiling then writes the assembly in such a way that when it accesses that memory location it only ever reads two bytes because it was defined as a Short at high level e.g

LDI r0, $0x0002

Or does it keep a lookup somewhere of memory locations and their types?

I'm hoping my question makes sense but please let me know if it doesn't and I'll do my best to clear any questions up.

Many Thanks

Nick

1

1 Answer 1

5

In a high-level language, the compiler (or interpreter) remembers for every variable what type it is and where it is stored. The type of a variable determines its size and what operations can be performed on the variable.

When generating the assembly (or machine code) instructions, the compiler chooses the correct instructions for the size and type of variable that is involved in the calculation. Depending on the instruction set of the processor, this can be a sequence of multiple instructions.

Each instruction of the processor is designed to work with a specific size of operands (usually 8, 16, 32 or 64 bits) and those operands can be either memory locations or registers or a combination thereof. From the specific instruction, the CPU knows how much memory to access (if the instruction operates on a memory address) or how many bits from a register to use and which register.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.