【发布时间】:2012-10-12 00:44:17
【问题描述】:
这看起来应该可以,但是在我输入值后我不断收到分段错误。有什么建议吗?
.section .data
speed: .int 0
fine: .int 0
points: .int 0
inputPrompt: .asciz "Enter an integer value for speed ==> "
outputPrompt: .asciz "With a speed of %d the fine will be $%d and %d points will be added to your license"
inputSpec: .ascii "%d"
.section .text #read in values
.globl main
main:
nop
pushl $inputPrompt
call printf
addl $4, %esp
#read in speed value
pushl $speed
pushl $inputSpec
call scanf
addl $8, %esp
#-------------------greater than or equal to 86--------------------------
movl speed, %eax
subl $85, %eax
Jg fine4
#-------------------81 - 85---------------------------------------------
movl speed, %eax
subl $80, %eax
Jg fine3
#-------------------76 - 80---------------------------------------------
movl speed, %eax
subl $75, %eax
Jg fine2
#-------------------71 - 75---------------------------------------------
movl speed, %eax
subl $70, %eax
Jg fine1
#-----------------less than 71-----------------------------------------------
movl $0, fine
movl $0, points
JMP output
#---------------------71 - 75-------------------------------------------
fine1:
movl $60, fine
movl $2, points
JMP output
#---------------------76 - 80-------------------------------------------
fine2:
movl $90, fine
movl $3, points
JMP output
#---------------------81 - 85-------------------------------------------
fine3:
movl $120, fine
movl $4, points
JMP output
#---------------------less than or equal to 86------------------------------------------
fine4:
movl $150, fine
movl $6, points
#----------------------------------------------------------------
output:
pushl points
pushl fine
pushl speed
pushl outputPrompt
call printf
addl $8, %esp
#-----------------------------------------------------------------
call exit
【问题讨论】:
-
inputSpec 是 .ascii 而不是 .asciz 有什么原因吗? scanf 会认为这是空终止的,这可能是 seg 错误的原因。
-
是不是调用scanf的时候崩溃了?如果不是,那么它到底在哪条指令上。如果是,那么它可能来自未对齐的堆栈,或者来自 James 提到的缺少 nul 终止。
-
我将 inputSpec 更改为 .asciz,但我仍然在看似相同的地方遇到分段错误。我在终端中运行它,所以它没有告诉我我的错误来自哪里,我在 scanf 之后放置了
pushl speed pushl testPrompt # --> testPrompt: .asciz "the value is %d" call printf addl $8, %esp以测试它是否甚至超过了那个点并且它保持分段错误而没有新的输出 -
你用的是什么汇编程序?你是如何运行代码的?
-
有一个
cmp指令可以省去在sub之后重新加载原件的麻烦。它设置标志与sub相同,但不写入目标寄存器。由于您的间隔都相同,您还可以检查 > 71,然后将该数字映射到查找表索引(减去 71 并除以 5,然后钳制最大索引。)