Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bits bytes words and dwords - when to use what in assembly?

I am trying to learn assembly (more like struggling) and I have come across the data types bytes, words, dwords etc.

What really confuses me is when do you know to use one over the other. Obviously you want to use the most conservative option, but how do I know to choose a byte over a word? For example 1 word = 16 bits or 2 bytes. Does that mean I can fit 16 characters inside 1 word? So to save the alphabet I would need to declare 2 words?

Same goes for registers (16 bit, 32 bit and 64 bit). I know you can use smaller registers such as al and ah instead of eax when you are manipulating smaller sizes of data. For a 16 bit register can you move a data type larger than a word into it because since it's 16 bits, it can fit a 16 bit word?

Wrapping my head around this whole concept is difficult. If anybody happens to have any good resources or information I can look at, that would be great. Cheers!

like image 572
user99545 Avatar asked Feb 28 '12 02:02

user99545


People also ask

When you would use bits and when you would use bytes?

The distinction between these two is probably the most important for you. Bits equals your internet connection speed and bytes equals an amount data.

What is the difference between bits bytes and words?

A byte is 8 bits and a word is the smallest unit that can be addressed on memory.

How many bytes is a word in assembly?

Fundamental Data Types A byte is eight bits, a word is 2 bytes (16 bits), a doubleword is 4 bytes (32 bits), and a quadword is 8 bytes (64 bits).

What is bit byte nibble and word?

Bit: 1 digit. Nibble: 4 digits. Byte: 8 digits. Word: The standard memory bus width in your architecture.


2 Answers

Choosing the right data width to use can be hard at first, but it'll be easy once you get used to it. Here are some questions to consider, with the most important considerations mentioned first.

Will the algorithm behave correctly? Say you wanted to compute 180 × 247 and print the result. Each operand fits in an unsigned byte, but the product doesn't. Or say you wanted to represent the size of a file, which are easily many megabytes large. Then you will need something wider than a 16-bit integer to represent the size.

What is the input/output format? If you expect other code to call your function with an array of bytes, then you must read the data elements as bytes; there is no other choice. You can convert to a wider representation temporarily for processing, though.

How much memory will it use? If you have a billion database records and each record uses a byte, then the total data size would be 1 GB. If each record used a dword, then the total data size would 4 GB. Small differences when multiplied by huge numbers will give huge differences.

In summary, it doesn't really matter what data width you use, as long as you can write correct code, interface with other code, and keep memory usage small enough that you can run your program. So using dword as a default wouldn't be a bad idea.

like image 67
Nayuki Avatar answered Sep 20 '22 13:09

Nayuki


A bit is not a character. It's a single binary digit, 0 or 1. Two bits are either 00, 01, 10 or 11. Read up on binary arithmetic (and conversion to/from decimal to verify your answers) and representations of data types. This is basic stuff, do not go on without being absolutely sure what a byte is and what values it may contain and what you might use it for.

word is a data type, 16 bits or 2 bytes in our case. So when we paraphrase your question, it becomes:

For a two byte register can you move a data type larger than two bytes into it because since it's two bytes, it can fit a two byte space?

This is the incoherence mentioned by @Ignacio Vazquez-Abrams.

like image 30
aib Avatar answered Sep 19 '22 13:09

aib