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 PortalCOMP3137String Constants
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
    8 min read
    Topic 34 of 73
    Topic Study Note Illustration
    34

    String Constants

    In assembly language, string constants represent sequences of characters or text. These constants are essential for tasks like text output, storing messages, or processing data in text...

    String Constants in Assembly Language

    In assembly language, string constants represent sequences of characters or text. These constants are essential for tasks like text output, storing messages, or processing data in text form. A string constant in assembly is typically an array of characters, where each character is stored as a byte (or a word for wider encodings like Unicode), and each character in the string corresponds to a specific ASCII value or other encoding.

    Representing String Constants

    In most assemblers, string constants are typically declared in the data segment of the program. The string can be either null-terminated (a special character to indicate the end of the string) or length-prefixed (where the length of the string is stored before the characters). The most common approach is using null-terminated strings.

    1. Null-Terminated Strings

    A null-terminated string ends with a special character, usually the ASCII value 0 (null character). This null character indicates the end of the string, so the program knows when to stop reading or processing characters.

    In assembly, strings are often declared using the DB (Define Byte) directive, and a 0 byte is used as the null terminator.

    Example (MASM Syntax):
    .data
        message  DB 'Hello, World!', 0  ; Declare a null-terminated string
    

    In this example:

    • 'Hello, World!' is the string.
    • The 0 at the end signifies the null terminator, marking the end of the string.

    When the string is processed by the program (e.g., for output), the program will continue reading until it encounters the null byte (0).

    2. Length-Prefixed Strings

    In length-prefixed strings, the length of the string (number of characters) is stored in the first byte, followed by the string itself. This approach is more efficient for some operations because you don't need to search for a null terminator.

    Example (MASM Syntax):
    .data
        message_length  DB 13    ; Length of string (13 characters)
        message         DB 'Hello, World!'
    

    In this example:

    • message_length stores the length of the string (13 characters).
    • The string itself ('Hello, World!') is stored in memory following the length byte.

    Accessing String Constants

    In assembly, to access individual characters of a string, you typically use memory addressing. This involves loading the address of the string into a register and then accessing specific elements one by one.

    Example (Accessing a String Character):

    .data
        message  DB 'Hello, World!', 0
    
    .code
        MOV SI, OFFSET message    ; Load the address of 'message' into SI register
        MOV AL, [SI]              ; Load the first character ('H') into AL
    

    Here:

    • SI holds the address of the string message.
    • [SI] accesses the first byte of the string ('H'), and it is loaded into the AL register.

    Iterating through a String

    To iterate through each character in a string (null-terminated), you can use a loop that continues until the null terminator is found.

    Example (Iterating Over a String in MASM):
    .data
        message  DB 'Hello, World!', 0  ; Null-terminated string
    
    .code
    start:
        MOV SI, OFFSET message    ; Load the address of 'message' into SI register
    
    print_loop:
        MOV AL, [SI]              ; Load the current character into AL
        CMP AL, 0                 ; Compare the character with 0 (null terminator)
        JE done                   ; If null terminator, jump to done
        ; Here you would put the code to print AL (e.g., to console)
        INC SI                    ; Move to the next character
        JMP print_loop            ; Repeat the loop
    
    done:
        ; End of string processing
    

    Special Characters in String Constants

    Sometimes you need to represent special characters in strings, such as newlines, tabs, backslashes, or quotes. These characters are typically represented using escape sequences.

    Common Escape Sequences:

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

    These escape sequences are often used when you need to include characters that are difficult to type directly, like the newline character or a tab character.

    Example (String with Escape Sequences):
    .data
        message  DB 'Hello, World!', 10, 0  ; 'Hello, World!' followed by newline (ASCII 10)
    

    In this example:

    • The string message contains the text 'Hello, World!', followed by a newline character (0x0A or 10 in decimal).
    • The null terminator (0) is used to mark the end of the string.

    Using Backslashes and Quotes in Strings:

    To include a backslash (\) or quotes ("), use escape sequences to avoid conflicts with the assembler syntax.

    .data
        message  DB 'This is a backslash: \', 0  ; String with a backslash
        quote_message DB 'She said, "Hello!"', 0  ; String with double quotes
    

    In these examples:

    • The backslash (\) is represented by the escape sequence \\.
    • The double quotes (") are represented by the escape sequence \".

    String Constants in Other Assemblers

    Other assemblers may have different ways of defining and using string constants. Here's how string constants are typically handled in some popular assemblers.

    NASM (Netwide Assembler) Syntax:

    NASM supports defining strings with db (define byte) or equ for constants. Strings are often defined using double quotes.

    section .data
        message db 'Hello, World!', 0  ; Null-terminated string
        quote_message db 'She said, "Hello!"', 0  ; String with quotes
    
    • db defines a byte (or multiple bytes) for the string.
    • The null terminator (0) is used to mark the end of the string.

    GAS (GNU Assembler) Syntax:

    In GAS, string constants are usually declared using .string or .asciz for null-terminated strings.

        .data
        message: .string "Hello, World!"  ; Null-terminated string
        quote_message: .asciz "She said, \"Hello!\""  ; String with quotes
    
    • .string is used to define a null-terminated string.
    • .asciz is another directive that explicitly denotes a null-terminated string.

    String Operations

    In assembly, you can perform various operations on strings, such as:

    • Concatenation (joining strings together).
    • Searching (finding substrings).
    • Copying (moving strings from one location to another).

    These operations usually require looping over the string and using instructions like MOV, CMP, LODS, STOS, etc.

    Example (Concatenating Strings):

    .data
        str1 DB 'Hello', 0
        str2 DB 'World', 0
        result DB 12 DUP (0)  ; Allocate space for result string
    
    .code
    start:
        ; Copy str1 to result
        MOV SI, OFFSET str1   ; Load address of str1
        MOV DI, OFFSET result ; Load address of result
    copy_str1:
        MOV AL, [SI]          ; Load character from str1
        CMP AL, 0             ; Check for null terminator
        JE copy_str2          ; Jump to copy_str2 if end of str1
        MOV [DI], AL          ; Store character in result
        INC SI                ; Move to next character in str1
        INC DI                ; Move to next position in result
        JMP copy_str1
    
    copy_str2:
        ; Copy str2 to result
        MOV SI, OFFSET str2   ; Load address of str2
    copy_str2_loop:
        MOV AL, [SI]          ; Load character from str2
        CMP AL, 0             ; Check for null terminator
        JE done               ; End if null terminator is reached
        MOV [DI], AL          ; Store character in result
        INC SI                ; Move to next character in str2
        INC DI                ; Move to next position in result
        JMP copy_str2_loop
    
    done:
        ; End of string concatenation
    

    Conclusion

    String constants in assembly language represent sequences of characters and are a fundamental part of string manipulation in low-level programming. These constants are typically defined in the data segment of the program and are often null-terminated. You can perform various operations on strings, such as iteration, comparison, and manipulation, but these operations require explicit handling of memory and register values.

    String constants in assembly offer a flexible way to handle textual data in applications, especially in embedded systems or low-level software, where string manipulation is an essential feature. Understanding how to work with strings efficiently is crucial for tasks like displaying messages, parsing text, or performing low-level I/O operations.

    ← Previous topic 33
    Character Constants
    Next topic 35 →
    Reserved Words

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language