MIPS操作数类型错误求助:字符串比较程序加载字符时报错
Hey there! Let's work through this MIPS string comparison problem together. That "operand of incorrect type" error when loading single characters is super common, and I bet it's tied to using the wrong load instruction—let's break it down and build a working solution step by step.
Why You're Seeing That Error
MIPS strings are made up of 1-byte ASCII characters, but if you're using lw (load word, which grabs 4 bytes) instead of lb (load byte) or lbu (load byte unsigned) to read individual characters, the assembler will throw that "incorrect type" error. lw expects a memory address aligned to 4 bytes and loads a full word, which doesn't match the 1-byte size of a single character.
Working MIPS String Comparison Example
Here's a complete program that loops through each character, compares them, and outputs whether the first string is greater than, less than, or equal to the second. I've added comments to explain each part:
.data str1: .asciiz "hello" # First input string (you can modify this or read from user input) str2: .asciiz "world" # Second input string msg_gt: .asciiz "str1 is greater than str2" msg_lt: .asciiz "str1 is less than str2" msg_eq: .asciiz "str1 is equal to str2" .text main: la $a0, str1 # Load address of first string into $a0 la $a1, str2 # Load address of second string into $a1 loop: # Load current characters from each string lb $t0, 0($a0) # Load byte (character) from str1 into $t0 lb $t1, 0($a1) # Load byte (character) from str2 into $t1 # Check if either string has ended beq $t0, $zero, check_end # If str1 is at null terminator, jump to check end beq $t1, $zero, str1_gt # If str2 is at null terminator but str1 isn't, str1 is longer (greater) # Compare the current characters bgt $t0, $t1, str1_gt # If str1 char > str2 char, str1 is greater blt $t0, $t1, str1_lt # If str1 char < str2 char, str1 is less # If characters are equal, move to next character addi $a0, $a0, 1 # Increment str1 pointer by 1 byte addi $a1, $a1, 1 # Increment str2 pointer by 1 byte j loop # Jump back to start of loop check_end: beq $t1, $zero, str_eq # If both strings are at null terminator, they're equal j str1_lt # If str1 ended but str2 hasn't, str1 is shorter (less) str1_gt: la $a0, msg_gt j print_msg str1_lt: la $a0, msg_lt j print_msg str_eq: la $a0, msg_eq print_msg: li $v0, 4 # Syscall code for printing string syscall li $v0, 10 # Exit program syscall
Key Fixes & Notes
- Use
lb/lbufor characters: This is the direct fix for your operand type error.lbhandles signed bytes (perfect for standard ASCII since most characters are positive), andlbuis better if you need to handle extended ASCII values. - Handle string termination: Don't forget to check for the null terminator (
\0, represented by$zeroin MIPS) — if one string ends before the other, the shorter one is considered "less than" the longer one. - Increment pointers correctly: Since each character is 1 byte, we use
addi $a0, $a0, 1to move to the next character (notaddi ... 4, which would skip 3 characters by mistake!).
If you're reading input from the user instead of using hardcoded strings, you can add code to use syscall 8 (read string) before the loop—just make sure to allocate enough space in the .data section for the input buffers.
内容的提问来源于stack exchange,提问作者user9241855

