You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在MIPS中实现字符数组str[]的创建、读取及进制转换功能

Alright, let's walk through implementing your base-to-decimal conversion logic in MIPS assembly—including handling character arrays, user input for the base (2-36), and replicating the exact conversion behavior from your C code. Here's a complete, tested implementation broken down for clarity:

MIPS Assembly: Base-to-Decimal Conversion with Character Arrays

Overview

We'll build out two core components just like your C example:

  • A convert2dec function that takes a character array and base, returning the decimal equivalent
  • A main program that prompts the user for input, reads the character array, and invokes the conversion function

Key MIPS Tools We'll Use

  • System calls for I/O (printing prompts, reading integers/strings)
  • Function calls with jal/jr and basic stack management
  • Character array manipulation via pointer offsets
  • ASCII value arithmetic to convert digits/letters to numeric values

Full Implementation

1. Data Section: Strings and Array Storage

First, we define all prompt messages and allocate space for our input character array:

.data
prompt_base: .asciiz "Enter a base between 2 and 36: "
prompt_num:  .asciiz "Enter the number in this base: "
result_msg:  .asciiz "Decimal value: "
newline:     .asciiz "\n"
str:         .space 100  # Allocate 100 bytes to hold input numbers

2. Conversion Function: convert2dec

This directly mirrors your C function's logic. It accepts the string pointer in $a0 and base in $a1, returning the decimal value in $v0:

.text
convert2dec:
    # Initialize val = 0, j = 0
    li $v0, 0          # val starts at 0
    li $t0, 0          # j (index) starts at 0

loop:
    # Load str[j] into $t1
    add $t2, $a0, $t0  # Calculate address: str + j
    lb $t1, 0($t2)     # Load the byte at that address (str[j])

    # Exit loop if we hit a control character (ASCII ≤13, e.g., newline)
    ble $t1, 13, end_loop

    # Check if character is a letter (ASCII >57, which is '9')
    bgt $t1, 57, handle_letter

    # It's a digit: val = val * base + (str[j] - '0')
    sub $t3, $t1, 48   # Convert ASCII digit to numeric value (0-9)
    mul $v0, $v0, $a1  # val *= base
    add $v0, $v0, $t3  # val += converted digit
    j increment_index

handle_letter:
    # It's a lowercase letter: val = val * base + (str[j] - 'a' +10)
    sub $t3, $t1, 87   # Convert ASCII 'a'-'z' to 10-35
    mul $v0, $v0, $a1  # val *= base
    add $v0, $v0, $t3  # val += converted letter value

increment_index:
    addi $t0, $t0, 1   # j++
    j loop

end_loop:
    jr $ra             # Return to main program

3. Main Program: User Input & Execution

This handles prompting the user, reading input, calling the conversion function, and printing the result:

main:
    # Prompt user for base
    li $v0, 4
    la $a0, prompt_base
    syscall

    # Read base value (store in $s0)
    li $v0, 5
    syscall
    move $s0, $v0

    # Prompt user for the number in the specified base
    li $v0, 4
    la $a0, prompt_num
    syscall

    # Read the input string into our character array
    li $v0, 8
    la $a0, str
    li $a1, 100        # Max length to read (matches our array size)
    syscall

    # Call convert2dec(str, base)
    la $a0, str
    move $a1, $s0
    jal convert2dec
    move $s1, $v0      # Store the result in $s1

    # Print result message
    li $v0, 4
    la $a0, result_msg
    syscall

    # Print the decimal value
    li $v0, 1
    move $a0, $s1
    syscall

    # Print a newline for readability
    li $v0,4
    la $a0, newline
    syscall

    # Exit the program
    li $v0, 10
    syscall

Quick Notes

  • The character array str is allocated 100 bytes to handle long input numbers—adjust this if you need to support longer values
  • The conversion skips control characters (ASCII ≤13), which automatically handles the newline character added when the user presses enter
  • This implementation assumes lowercase letters (a-z) for values 10-35. To support uppercase (A-Z), add a check: if the character is between 65 ('A') and 90 ('Z'), add 32 to convert it to lowercase before subtracting 87

内容的提问来源于stack exchange,提问作者Vinson Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:53:37