如何修复汇编代码中的分段错误?

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

我试图从用户那里反转一个 5 字符的字符串并将其指向后面,但程序只是打印原始字符串和“分段错误”。

.data // start of the data segment
input:
    .space 100
inputSize = .-input
prompt:
    .asciz "Enter a string with 5 characters"
promptSize = .-prompt
reversed:
    .space 100
reversedSize = .-reversed
.text // start o the text segment (Code)
.global _start // Linux Standard _start, similar to main in C/C++
_start:
// Display prompt message
mov x0, #0          // stdout
ldr x1, =prompt      // store the address of flow into x1
ldr x2, =promptSize      // store the size of the flowwWorld string into x2
mov x8, #64         // store 64 to x8, this is the linux write call
svc 0  
mov x0, #1 // file descriptor for stdin
ldr x1, =input      // store the address of flow into x1
ldr x2, =inputSize
mov x8, #63 // system call number for read
svc 0
mov x0, #1 // file descriptor for stdout
ldr x1, =input // address of input buffer
mov x2, #100 // maximum number of bytes to write
mov x8, #64 // system call number for write
svc 0
// Reverse the input string
mov x1, #0        // set x1 to 0 to represent the start of the input string
add x2, x2, #-2  // set x2 to the second-to-last index in the input string
mov x4, #0
loop: 
  cmp x4, #2
  b.gt end
  ldrb w3, [x1], #1  // load byte from input string starting from the left
  ldrb w4, [x2], #-1 // load byte from input string starting from the right
  strb w4, [x1, #-1] // store right byte at the left index
  strb w3, [x2, #1]  // store left byte at the right index
  add x4, x4, 1
  b loop             // jump to loop
end:
// Display the reversed string
mov x0, #0          // stdout
ldr x1, =input      // store the address of input into x1
ldr x2, =inputSize  // store the size of the input string into x2
mov x8, #64         // store 64 to x8, this is the linux write call
svc 0
// Exit to the OS, essentially this code does this in c
// return 0;
mov x0, #0          // return value
mov x8, #93         // Service call code
svc 0   

为了反转字符串,我尝试交换外部的两个字符,然后交换内部的两个字符,将中间的字符留在原处。

assembly arm64
© www.soinside.com 2019 - 2024. All rights reserved.