ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137Data Representation Co…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    9 min read
    Topic 67 of 73
    Topic Study Note Illustration
    67

    Data Representation & Conversion

    In assembly language, data representation and conversion are critical concepts that deal with how data is stored, manipulated, and transformed from one format to another. Different types...

    Data Representation & Conversion in Assembly Language

    In assembly language, data representation and conversion are critical concepts that deal with how data is stored, manipulated, and transformed from one format to another. Different types of data are represented in the computer system in various ways depending on the system architecture, the instruction set, and the nature of the data itself. These representations are used to store integers, floating-point numbers, characters, strings, and other complex data types.

    Understanding how data is represented and how conversions between different representations work is crucial for writing efficient assembly code and for ensuring that data is processed correctly.

    1. Data Representation in Computers

    Binary Representation

    At the lowest level, all data in a computer is represented in binary (base 2), meaning that everything is stored as sequences of 0s and 1s. The binary system is the foundation of all data representation in computers.

    • 1 bit represents a binary digit, either 0 or 1.
    • Byte: A sequence of 8 bits is called a byte. A byte can represent 256 distinct values (ranging from 0 to 255 for unsigned data or -128 to 127 for signed data).

    For example:

    • 0b11001001 is a binary number that can be used to represent an integer or part of a more complex data type.

    Integer Representation

    • Signed Integer: In the binary system, signed integers are typically represented using two's complement notation, which allows for negative numbers.

      • Example of a signed 8-bit number: 0xF7 (which is -9 in decimal using two's complement representation).
    • Unsigned Integer: For unsigned integers, all bits represent the magnitude of the number, ranging from 0 to 2^n - 1, where n is the number of bits.

    Floating-Point Representation

    • Floating-point numbers are typically represented using the IEEE 754 standard, which divides the number into:

      • Sign bit (1 bit)
      • Exponent (8 bits for single precision, 11 bits for double precision)
      • Mantissa (23 bits for single precision, 52 bits for double precision)

      This allows for representing very large or very small numbers.

    Character Representation

    • ASCII (American Standard Code for Information Interchange) is a standard that represents characters as 7 or 8-bit values. For example:

      • A is 0x41 (or 01000001 in binary).
      • a is 0x61 (or 01100001 in binary).
    • Unicode: For non-English characters and symbols, Unicode provides a larger set of values to represent characters, often using multiple bytes (16 bits or more per character).


    2. Data Conversion

    Data conversion involves transforming data from one representation to another. This process is necessary when interacting with different formats or when performing operations on data types that require specific representations.

    Integer to Binary and Binary to Integer

    • Integer to Binary Conversion: In assembly, integers are typically stored in binary form. When converting an integer to binary, you simply express the number as a series of 0s and 1s.

      Example: Decimal 13 is represented as binary 1101.

    • Binary to Integer Conversion: To convert binary back to decimal, you sum the values of the bits that are set (i.e., the 1 bits) based on their place value.

      Example: 1101 in binary = 1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 8 + 4 + 0 + 1 = 13 in decimal.

    Decimal to Binary and Binary to Decimal Conversion

    Decimal numbers are converted to binary using a method of successive division by 2.

    • Decimal to Binary (Unsigned Integer):

      • Divide the decimal number by 2.
      • Write down the remainder.
      • Continue dividing the quotient by 2, writing down the remainders, until the quotient is 0.
      • The binary representation is the sequence of remainders read in reverse order.

      Example: Convert decimal 19 to binary:

      • 19 ÷ 2 = 9 remainder 1
      • 9 ÷ 2 = 4 remainder 1
      • 4 ÷ 2 = 2 remainder 0
      • 2 ÷ 2 = 1 remainder 0
      • 1 ÷ 2 = 0 remainder 1

      So, 19 in binary is 10011.

    • Binary to Decimal: To convert binary to decimal, multiply each bit by the power of 2 corresponding to its position, then sum the results.

      Example: Convert binary 10011 to decimal:

      • 1×24+0×23+0×22+1×21+1×201 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^01×24+0×23+0×22+1×21+1×20
      • 1×16+0×8+0×4+1×2+1×1=16+0+0+2+1=191 \times 16 + 0 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1 = 16 + 0 + 0 + 2 + 1 = 191×16+0×8+0×4+1×2+1×1=16+0+0+2+1=19

    Hexadecimal and Decimal Conversion

    Hexadecimal (base 16) is another common representation used in assembly because it is more compact than binary. It is also easier for humans to read. Each hexadecimal digit represents exactly 4 binary digits (bits).

    • Decimal to Hexadecimal: Divide the decimal number by 16, noting the quotient and remainder. The remainder gives you the hexadecimal digits.

      Example: Convert decimal 255 to hexadecimal:

      • 255 ÷ 16 = 15 remainder 15, which is F in hexadecimal.
      • 15 ÷ 16 = 0 remainder 15, which is F in hexadecimal.

      So, 255 in hexadecimal is FF.

    • Hexadecimal to Decimal: Multiply each hexadecimal digit by the corresponding power of 16.

      Example: Convert FF in hexadecimal to decimal:

      • F×161+F×160=15×16+15×1=240+15=255F \times 16^1 + F \times 16^0 = 15 \times 16 + 15 \times 1 = 240 + 15 = 255F×161+F×160=15×16+15×1=240+15=255

    Signed and Unsigned Numbers

    When converting between signed and unsigned integers, you need to consider whether the number uses two's complement representation (signed) or just the magnitude of the number (unsigned).

    • Signed to Unsigned: When converting a signed number to unsigned, you need to ensure that the sign bit is properly interpreted. For instance, if a 32-bit signed integer 0xFFFFFFFF (which is -1 in two's complement) is treated as an unsigned integer, it becomes 0xFFFFFFFF, which is 4,294,967,295 in decimal.

    • Unsigned to Signed: When converting an unsigned number to a signed one, you need to check if the value exceeds the maximum value for the signed type (e.g., 0x80000000 in a 32-bit signed integer would be considered negative).


    3. Common Data Representation Formats in Assembly

    • Signed Integers: Typically represented using two's complement in modern systems, which allows negative values to be represented using the most significant bit (MSB) as a sign bit.

    • Unsigned Integers: Represented directly with all bits used for magnitude.

    • Floating Point Numbers: Use the IEEE 754 standard, which breaks the number into a sign, exponent, and mantissa.

    • Characters: Stored using ASCII (or Unicode) codes, where each character corresponds to a unique numerical value.


    4. Data Conversion in MASM

    MASM provides a number of tools and instructions that allow you to convert between different data formats.

    For example, converting between decimal and binary numbers in MASM can be done using the DEC and BIN directives. These can be used in conjunction with printf-like functions to display numbers in different bases.

    mov ax, 10         ; Store decimal number
    mov bx, 0x0A       ; Store hexadecimal number
    mov cx, 100        ; Store binary number
    

    Example of Converting Integer to String in MASM:

    .data
        num db 10   ; decimal 10
        str db 6 dup(0)
    
    .code
        mov ax, num    ; Load number into ax
        call itoa      ; Convert integer to ASCII string
    

    In this example, itoa (integer to ASCII) is a function that converts an integer to its string representation.


    5. Summary

    • Data Representation in computers is typically done in binary, hexadecimal, and sometimes floating-point formats. Different data types (integers, characters, floating-point numbers) require different representations.
    • Conversions between formats such as decimal to binary, binary to hexadecimal, and signed to unsigned are common tasks in assembly programming.
    • Data types in assembly are represented as binary numbers, and the conversion between them is an essential skill for low-level programming.
    • MASM provides built-in methods and instructions to convert data from one format to another, which is vital for tasks like handling input/output or working with multiple data types in a program.

    Understanding data representation and conversion is key for working

    ← Previous topic 66
    Compound Expressions
    Next topic 68 →
    Architecture

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    9mremaining
    1.5kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language