In assembly language, WORD and SWORD refer to data types that store 16-bit values. These types differ in how they handle signed and unsigned values. Let's break down the concepts of WORD and SWORD, their ranges, and how they're used in assembly programming.
The WORD data type represents a 16-bit unsigned integer, meaning it can store values in the range of 0 to 65535. A word consists of 2 bytes (16 bits), and it is used for storing unsigned values—those that are always non-negative.
Since WORD is unsigned, it doesn't have any sign bit, so all 16 bits are used to represent the magnitude of the number.
name DW value
DW (Define Word) is used to define a 16-bit word variable.section .data
wordValue DW 30000 ; Declare a 16-bit unsigned word with value 30000
anotherWord DW 65535 ; Declare a 16-bit unsigned word with the maximum value (65535)
In this example:
wordValue holds 30000, which is within the valid range for an unsigned 16-bit number.anotherWord holds 65535, the maximum value that can be stored in an unsigned 16-bit word.The SWORD data type represents a 16-bit signed integer, meaning it can store both positive and negative values. In a signed word, one bit (the most significant bit, or MSB) is used to represent the sign of the number (positive or negative). The remaining 15 bits represent the magnitude of the number.
In a signed 16-bit word, the most significant bit (MSB) is the sign bit:
This is known as two's complement representation, where negative numbers are encoded by subtracting the positive number from 65536 (in 16-bit form) and flipping the bits.
name DW value
DW (Define Word) is also used to define a signed 16-bit word variable. The value provided is interpreted as signed.section .data
signedWord DW -30000 ; Declare a signed 16-bit word with value -30000
anotherSigned DW 32767 ; Declare a signed 16-bit word with value 32767 (maximum positive)
In this example:
signedWord holds -30000, which is a negative number within the valid range for a signed 16-bit number.anotherSigned holds 32767, which is the maximum positive value for a signed 16-bit number.The primary difference between WORD and SWORD lies in how they treat the values they store:
| Feature | WORD (Unsigned Word) | SWORD (Signed Word) |
|---|---|---|
| Size | 16 bits (2 bytes) | 16 bits (2 bytes) |
| Range | 0 to 65535 (decimal) | -32768 to 32767 (decimal) |
| Sign | Only non-negative values (0 to 65535) | Can represent both positive and negative values |
| Use Case | When you know values will be non-negative (e.g., memory addresses, image pixel values) | When you need to represent signed integers (e.g., temperatures, adjustments) |
| Hexadecimal Range | 0000 to FFFF | 8000 to 7FFF (negative), 0000 to 7FFF (positive) |
Use WORD (unsigned) when you know that the data will always be non-negative. This could be data like:
Use SWORD (signed) when you need to store both positive and negative values, like:
Let’s say we are working with a counter that tracks the number of items in a warehouse. The count can never be negative, so we will use WORD (unsigned) to store the value.
section .data
itemCount WORD 1000 ; Declare an unsigned word with value 1000 (number of items)
itemCount holds 1000, representing the number of items in the warehouse.Now, let’s say we need to store the temperature of a room, which can be both positive and negative. We will use SWORD (signed) to store the value.
section .data
roomTemperature SWORD -15 ; Declare a signed word with the value -15 (temperature in Celsius)
roomTemperature holds -15, indicating that the room temperature is negative.Sometimes, you might need to mix both signed and unsigned data. For instance, we might define both wordCount (which is unsigned) and temperature (which is signed):
section .data
wordCount WORD 5000 ; Unsigned word to hold the number of words
temperature SWORD -5 ; Signed word to hold the temperature in Celsius
wordCount holds 5000, representing the count of words (non-negative).temperature holds -5, representing the room temperature (signed).Understanding the difference between these two types is critical in assembly language programming, as it helps you choose the correct type depending on whether you need to handle signed or unsigned values.
Open this section to load past papers