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›Character Constants
    Computer Organization and Assembly LanguageTopic 33 of 73

    Character Constants

    6 minread
    1,028words
    Intermediatelevel

    Character Constants in Assembly Language

    In assembly language, character constants represent individual characters or sequences of characters that can be used directly in the program. These constants are typically used for tasks like manipulating text, storing strings, or performing operations that involve characters (such as comparisons or searching for specific characters).

    Character constants are typically represented by their ASCII values (or another character encoding like Unicode). Each character is associated with a unique numeric value, which corresponds to the character's representation in the computer's memory. The most commonly used encoding system for characters is ASCII (American Standard Code for Information Interchange), but other encodings like Unicode may be used in certain systems.

    How Character Constants are Represented

    Character constants in assembly are generally represented using single quotes (') or double quotes ("), depending on the assembler syntax. The constant can be either a single character or a string of characters, and the assembler will often convert these characters into their corresponding ASCII (or other character set) values.

    1. Single Character Constant

    A single character constant is typically a byte (8 bits) value that corresponds to one character in the ASCII table. In assembly, these are written inside single quotes.

    • Example (MASM Syntax):

      MOV AL, 'A'   ; Load the ASCII value of 'A' (65) into AL register
      

      Explanation:

      • 'A' represents the character 'A', which corresponds to the ASCII value 65.
      • The MOV AL, 'A' instruction moves this ASCII value into the AL register.

    2. String Constants (Multiple Characters)

    A string constant consists of a sequence of characters. In assembly, a string is typically stored as an array of individual characters, each represented by a byte (or word for wider encodings, like Unicode). In MASM (Microsoft Macro Assembler), strings are usually enclosed in double quotes.

    • Example (MASM Syntax):

      .data
        message  DB 'Hello, World!', 0  ; Define a null-terminated string
      

      Explanation:

      • DB stands for Define Byte, and it stores the string 'Hello, World!' in memory.
      • The 0 at the end of the string is a null terminator, which indicates the end of the string in many programming languages and assembly operations.
      • Each character in the string is stored as its ASCII value (e.g., 'H' = 72, 'e' = 101, etc.).

    Character Constants and ASCII Values

    In most assembly languages, each character is internally represented by a numeric ASCII code. The ASCII table maps characters to numeric values. For example:

    Character ASCII Value (Decimal) ASCII Value (Hex)
    'A' 65 0x41
    'B' 66 0x42
    'a' 97 0x61
    'z' 122 0x7A
    '0' 48 0x30
    ' ' (Space) 32 0x20
    '\n' 10 0x0A

    You can use the numeric ASCII values in place of the characters directly in your assembly code, especially when working with mathematical or logical operations on characters.

    Example (MASM with ASCII values):

    MOV AL, 65    ; Load the ASCII value 65 (which represents 'A') into AL
    MOV BL, 0x41  ; Load the hexadecimal value 0x41 (which also represents 'A') into BL
    

    Character Constants and Escaped Characters

    Some special characters cannot be represented directly with a single printable symbol (like newline \n, tab \t, or backslash \\). These characters are often written using escape sequences.

    Common Escape Sequences in strings:

    • \n: Newline (ASCII 10)
    • \t: Horizontal tab (ASCII 9)
    • \\: Backslash (ASCII 92)
    • \': Single quote (ASCII 39)
    • \": Double quote (ASCII 34)

    In some assemblers (such as NASM or GNU Assembler), you can directly use escape sequences inside strings.

    Example (Escape Sequences in String Constants):

    .data
        newline    DB 'Hello, World!', 10  ; String with newline at the end
        tabbed     DB 'Item 1', 9, 'Item 2', 0  ; String with tab between items
    

    In this example:

    • The string newline contains the ASCII value 10 (newline) at the end, which will make it print on a new line.
    • The string tabbed uses 9 (tab) between two items, which will cause the output to have a tab space between Item 1 and Item 2.

    Manipulating Character Constants

    You can perform operations on character constants just like with integers, using arithmetic and logical operators. For example, you can compare characters, convert case, or perform bitwise operations.

    Example: Comparing Characters

    MOV AL, 'A'     ; Load 'A' (ASCII 65) into AL
    MOV BL, 'B'     ; Load 'B' (ASCII 66) into BL
    CMP AL, BL      ; Compare AL with BL (AL == 'A', BL == 'B')
    JE  Same        ; Jump to Same if AL == BL (characters are the same)
    

    Example: Converting Lowercase to Uppercase

    Since the ASCII values for uppercase and lowercase letters are offset by 32, you can convert a lowercase character to uppercase by subtracting 32.

    MOV AL, 'b'     ; Load 'b' (ASCII 98) into AL
    SUB AL, 32      ; Convert to uppercase (ASCII 66, which is 'B')
    

    Example: Bitwise Operations on Characters

    You can also perform bitwise operations on characters, such as clearing a particular bit or setting a bit.

    MOV AL, 'A'     ; Load 'A' (ASCII 65) into AL
    OR AL, 32       ; Set the lowercase bit (convert 'A' to 'a')
    

    Character Constants in Other Assemblers

    Other assemblers may use slightly different syntax for character constants.

    • NASM (Netwide Assembler): NASM supports single-character constants inside single quotes ('A'), and strings are written in double quotes.

      Example:

      section .data
          message db "Hello, World!", 0
          newline db 0x0A   ; ASCII value for newline
      
    • GAS (GNU Assembler): In GAS, you can also define character constants using the db directive.

      Example:

      .data
          char_1: .byte 'A'  ; Single character constant
          string: .string "Hello, World!"
      

    Conclusion

    Character constants in assembly language are fundamental for handling text and performing operations on individual characters. They are usually represented by their ASCII values and are used for things like string manipulation, comparisons, and text processing. Understanding how to work with characters and character constants is essential for low-level programming tasks, such as handling input and output, processing text files, and implementing algorithms that involve string matching or parsing.

    By using character constants, escape sequences, and bitwise operations, you can manipulate text efficiently in assembly, even in environments with limited resources or hardware.

    Previous topic 32
    Real Number Constants
    Next topic 34
    String Constants

    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 time6 min
      Word count1,028
      Code examples0
      DifficultyIntermediate