ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›Data Representation & Conversion
    Computer Organization and Assembly LanguageTopic 67 of 73

    Data Representation & Conversion

    9 minread
    1,482words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time9 min
      Word count1,482
      Code examples0
      DifficultyIntermediate