ARM 汇编:在 ARMv7 CPUlator 中将字符从读 FIFO 回显到写 FIFO。 JTAG UART 接口

问题描述 投票:0回答:1

我在将不在括号中的字符从“读取 FIFO”回显到“写入 FIFO”时遇到问题。这些字符在 JTAG UART 接口中输入。正在读取“读取 FIFO”中的字符,但未进行回显。


.section .text
.global _start
 
.equ UART_BASE, 0xFF201000
.equ READ_FIFO_OFFSET, 0x00
.equ WRITE_FIFO_OFFSET, 0x04
.equ NUM_CHARS_TO_READ, 16   // Adjust based on your system
 
_start:
    // Infinite loop
    B loop
 
loop:
    // Read characters from the UART Read FIFO
    LDR R1, =UART_BASE
    LDR R2, =READ_FIFO_OFFSET
    LDR R3, =NUM_CHARS_TO_READ
read_loop:
    LDRB R0, [R1, R2]  // Read a character from the Read FIFO
    CMP R0, #0      // Check if the character is null (end of string)
    BEQ loop_end
 
    // Check if the character is a double quote (ASCII 0x22)
    CMP R0, #0x22
    BNE check_next_char
 
    // Start echoing characters until the next double quote
    B echo_chars
 
check_next_char:
    // Increment the offset for the next character in the loop
    ADD R2, R2, #1
    SUBS R3, R3, #1 // Decrement the character count
    BNE read_loop   // Continue reading if there are more characters
 
loop_end:
    // Retry reading from the UART (you might want to add a delay here)
    B loop
 
echo_chars:
    // Echo characters to the Write FIFO until the next double quote
    LDR R2, =WRITE_FIFO_OFFSET
echo_loop:
    STRB R0, [R1, R2]  // Echo the character to the Write FIFO
    ADD R2, R2, #1
    CMP R0, #0x22   // Check if the echoed character is a double quote
    BEQ loop        // If double quote, go back to reading
 
    // Increment to the next character in the loop
    ADD R2, R2, #1
    SUBS R3, R3, #1
    CMP R3, #0
    BNE read_loop   // Continue reading if there are more characters
 
    // If we reach here, we have echoed characters until the next double quote
    B loop


无法正确回显字符

arm cpu
1个回答
0
投票

您提供的代码存在一些问题:偏移量处理不正确、括号处理逻辑以及读写逻辑不正确:

偏移处理不正确:

check_next_char 中的语句将错误地将您正在读取或写入的地址移至 UART 外设的内存空间内。

check_next_char:
// Increment the offset for the next character in the loop
ADD R2, R2, #1
SUBS R3, R3, #1 // Decrement the character count
BNE read_loop   // Continue reading if there are more characters

括号处理逻辑不正确:

read_loop 中的语句正在检查双引号(“ ASCII 值 0x22)而不是括号。

// Check if the character is a double quote (ASCII 0x22)
CMP R0, #0x22
BNE check_next_char

读写逻辑

read_loop 中的语句错误地尝试增加偏移量。

LDRB R0, [R1, R2]  // Read a character from the Read FIFO
CMP R0, #0      // Check if the character is null (end of string)
BEQ loop_end

// Check if the character is a double quote (ASCII 0x22)
CMP R0, #0x22
BNE check_next_char

// Start echoing characters until the next double quote
B echo_chars

您可以重构 ARM 程序集以从 UART FIFO 读取字符并回显括号外的字符:

.section .text
.global _start

.equ UART_BASE, 0xFF201000
.equ READ_FIFO_OFFSET, 0x00
.equ WRITE_FIFO_OFFSET, 0x04
.equ NUM_CHARS_TO_READ, 16   // Adjust based on your system

_start:
    // Infinite loop
    B loop

loop:
    // Read characters from the UART Read FIFO
    LDR R1, =UART_BASE
    LDR R2, =READ_FIFO_OFFSET
    LDR R3, =NUM_CHARS_TO_READ
    MOV R4, #0              // Flag to track if inside parentheses (0 = outside, 1 = inside)

read_loop:
    LDRB R0, [R1, R2]  // Read a character from the Read FIFO
    CMP R0, #0x28    // Check if the character is '(' (ASCII 0x28)
    BEQ inside_parentheses

    CMP R0, #0x29    // Check if the character is ')' (ASCII 0x29)
    BEQ outside_parentheses

    CMP R4, #1       // Check if currently inside parentheses
    BEQ skip_char    // If inside parentheses, skip the echo

    STRB R0, [R1, #WRITE_FIFO_OFFSET]  // Echo the character to the Write FIFO

skip_char:
    SUBS R3, R3, #1  // Decrement the character count
    BNE read_loop    // Continue reading if there are more characters

    B loop           // Restart the loop

inside_parentheses:
    MOV R4, #1       // Set flag to indicate inside parentheses
    B read_loop

outside_parentheses:
    MOV R4, #0       // Clear flag to indicate outside parentheses
    B read_loop
© www.soinside.com 2019 - 2024. All rights reserved.