【发布时间】:2014-05-16 05:25:11
【问题描述】:
我正在尝试对循环中的字符串的 ascii 值求和。我想我遗漏了一些东西,但是我总是在程序结束时返回错误的值(即与我使用 ascii 表手动求和的值不匹配),或者是段错误。
我的程序:
.data # .data section begins
name: .ascii "Json" # name is ASCII chars (.ascii or .byte)
len: .int 4 # name length is 4 chars, an integer
newline: .ascii "\n" # new line character
count: .int 0 # counter for loop (start at 0)
return: .int 0 # return code value
tmp: .int 0 # temp value
.text # text section starts
.global _start # main program entry
_start: # start instruction
again: # begin loop
mov $1, %edx # 1 byte at a time
mov $name, %ecx # mem addr
add count, %ecx # offset in string
#### Trouble area ####
# this seg faults
mov count(%ecx), %ebx
add %ebx, return
#### Trouble area ####
mov $1, %ebx # file descriptor 1 is computer display
mov $4, %eax # system call 4 is sys_write (output)
int $128 # interrupt 128 is entry to OS services
add $1, count # incr count
mov count, %eax # copy count to eax
cmp %eax, len # and compare values
jne again # if not equal, goto again
mov $newline, %ecx # mem addr
mov $1, %ebx # file desc 1
mov $4, %eax # sys call 4
int $128 # interrupt 128
mov $1, %eax # system call 1 is sys_exit
mov return, %ebx # status return
int $128 # interrupt 128
我认为我缺少一些基本的东西,但我理解这是将ecx 寄存器中的值移动count 的值(即循环迭代和数组元素)到寄存器@987654324 @。然后将ebx 中的值添加到return。然后在程序结束时,将return 中的求和值移动到寄存器ebx,然后调用中断。在运行时这个段错误,但我不确定为什么。
我希望它不会出现段错误(显然是大声笑),并且在调用 ~]# echo $? 以打印 410 时(即,'J' + 's' + 'o' + 'n' 或.. . 74 + 115 + 111 + 110)。
UPDATE:------------>
我已根据@Michael 的建议将“问题点”更新为以下内容(如果我理解正确的话)。
#### Trouble area ####
# this returns a garbage number, 154 instead of 410
mov count, %edi
mov name(%edi), %ebx
add %ebx, return
#### Trouble area ####
【问题讨论】:
-
我不确定你认为
mov count(%ecx), %ebx做了什么。它将尝试做的是获取count的地址,添加ecx的值,从结果地址加载一个32 位值并将其存储在ebx中。换一种说法;您正在访问count,就像它是一个数组一样,即使它被声明为单个int。 -
@Michael 谢谢。我的印象是
someNumber(%someregister)使用的是基指针寻址模式,即someNumber是您在(%someregister)中的内存地址位置的偏移量。我想我正在尝试像访问数组一样访问它...我尝试使用索引寻址模式,即mov name(,%ecx,1), %edi,但这也不起作用。 -
@Michael 我刚刚发布了对我的原始代码的修订(这次只是有问题的 sn-p。我不再遇到段错误,而是回到获得垃圾输出(错误的数字) .
-
你说的“输出”是什么意思?您似乎没有在代码中的任何位置打印任何数字。
-
@Michael 抱歉,我的意思是返回值。运行程序后,从终端发出
~]# echo $?应该获取该返回值,即。 410.跨度>