Search code examples
assemblyarmarm64

I am trying to write an ARM 64 Assembly program to implement a SELECT/CASE construct


I am creating an ARM Assembly Language framework to implement a SELECT/CASE construct.

The format is:

SELECT number
    CASE 1:
        << statements if number is 1 >>
    CASE 2:
        << statements if number is 2 >>
    CASE ELSE:
        << statement if not any other case >>

This is what I have tried but the program does not output the messages based on the entered number:

// Declare program entry point
.global _start

// Messages to be displayed
.data
prompt:     .asciz "Input a number: "  // Prompt message
case1_msg:  .asciz "You entered 1\n"  // Case 1 message
case2_msg:  .asciz "You entered 2\n"  // Case 2 message
else_msg:   .asciz "You entered a value other than 1 and 2\n"  // Else message

.text
_start:
    // Display prompt and get user input
    mov     x0, #0          // Set output to stdout
    adr     x1, prompt      // Load prompt address
    mov     x2, #17         // Set prompt length
    mov     x8, #64         // Call Linux write function
    svc     #0              // Execute system call
    mov     x0, #0          // Set input to stdin
    mov     x8, #63         // Call Linux read function
    svc     #0              // Execute system call

    // Check user input against cases
    cmp     w0, #1          // Compare input with case 1
    beq     case1           // Branch to case 1 if equal
    cmp     w0, #2          // Compare input with case 2
    beq     case2           // Branch to case 2 if equal
    b       else_case      // Branch to else case if not equal to either case

case1:
    // Perform actions for case 1
    adr     x0, case1_msg   // Load case 1 message address
    mov     x8, #64         // Call Linux write function
    svc     #0              // Execute system call
    b       end_select     // Branch to end of case1

case2:
    // Perform actions for case 2
    adr     x0, case2_msg   // Load case 2 message address
    mov     x8, #64         // Call Linux write function
    svc     #0              // Execute system call
    b       end_select     // Branch to end of case2

else_case:
    // Perform actions for else case
    adr     x0, else_msg    // Load else message address
    mov     x8, #64         // Call Linux write function
    svc     #0              // Execute system call

end_select:
    // Terminate program
    mov     x0, #0          // Set return code to 0
    mov     x8, #93         // Call Linux exit function
    svc     #0              // Execute system call

Solution

  • I managed to solve the problem. The issue was that I was comparing an integer with a character. I converted the character into an integer and the compared and everything worked as expected. I appreciate everyone who contributed to help me figure out what was the problem. Below is the code I wrote.

    // Assembler program to implement a SELECT/CASE construct
    .global _start // Provide program starting address
    _start:
         //Display a message and prompt for input
         mov    X0, #0
         ldr    X1, =prompt // Load address of prompt message
         mov    X2, #74 // length of the prompt message
         mov    X8, #64 // call Linux
         svc    0  //display the message
    
         //Reading the user input
         mov    X0, #0
         ldr    X1, =input  // Load address of input buffer
         mov    X2, #10     // input size
         mov    X8, #63     // system call
         svc    0           // read input from keyboard
    
         // From ASCII to integer conversion
         ldrb   W0, [X1]    // load the character
         sub    W0, W0, #'0'// convert to integer
         cmp    W0, #1      // check if the value is 1
         b.eq   print_one   // if true print message for 1
         cmp    W0, #2      // check if it is 2
         b.eq   print_two   // print message for two
    
        // This will execute if the input is not 1 or 2
         ldr    X1, =unknown// "unknown" message string
         mov    X2, #49 // length of the string
         mov    X0, #1 // 1 = StdOut
         mov    X8, #64 // write system call
         svc    0 // system call to print
         b      exit // move to exit
    
    //this option will be executed if the user enters 1
    print_one:
         //print "one" message
         ldr    X1, =one // message string
         mov    X2, #29 // length of the string
         mov    X8, #64 // write system call
         svc    0  // call to output message
         b      exit  // move to exit
    
    print_two:
         // Print "two" message
         ldr    X1, =two // message string
         mov    X2, #29 // lenth of the string
         mov    X8, #64 // write system call
         svc    0  // call to output message
         b      exit  // move to exit
    
    exit:
         // Exiting program
         mov    X0, #0 // Use 0 return code
         mov    X8, #93 // Service code 93 terminates
         svc    0 // Call Linux to terminate
    
    .data
    //different messages to be displayed
    prompt:  .ascii "Implement a Select/Case structure:\n1. Case 1 actions\n2. Case 2 actions\n"
    input:   .skip 10      // Input buffer for read system call
    one:     .ascii "case 1 actions will be taken\n"
    two:     .ascii "case 2 actions will be taken\n"
    unknown: .ascii "input does not match with any case. Else actions\n"
    //end of the program