我正在创建一个ARM
汇编语言框架来实现SELECT/CASE
结构。
格式为:
SELECT number
CASE 1:
<< statements if number is 1 >>
CASE 2:
<< statements if number is 2 >>
CASE ELSE:
<< statement if not any other case >>
这是我尝试过的,但程序不根据输入的数字输出消息:
// 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
1条答案
按热度按时间zte4gxcn1#
我设法解决了这个问题。问题是我在比较一个整数和一个字符。我将字符转换为整数并进行比较,一切都按预期工作。我感谢所有帮助我找出问题所在的人。下面是我写的代码。