Assembly 32位英特尔汇编比较和跳转

Assembly 32位英特尔汇编比较和跳转,assembly,intel,32-bit,Assembly,Intel,32 Bit,这看起来应该行得通,但在输入值之后,我一直会遇到分段错误。有什么建议吗 .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

这看起来应该行得通,但在输入值之后,我一直会遇到分段错误。有什么建议吗

.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

您缺少一个
$
登录
pushl outputPrompt
。该指令将
outputPrompt
的前4个字节放在堆栈上,但您需要地址。因此,请使用
pushl$outputPrompt


此外,还要学习使用调试器,以便您可以修复自己的错误。

inputSpec是.ascii而不是.asciz有什么原因吗?scanf预计这将以null结尾,这可能是seg故障的原因。在调用scanf期间是否发生崩溃?如果没有,具体是在哪个指令上。如果是,那么它可能来自未对齐的堆栈,或者来自James提到的缺少nul终止。我将inputSpec更改为.asciz,并且我仍然在相同的位置遇到分段错误。我在终端中运行它,所以它不会告诉我错误来自何处,我将
pushl speed pushl testPrompt\#-->testPrompt:.asciz“值为%d”在扫描后调用printf addl$8,%esp
来测试它是否超过了该点,并且它在没有新输出的情况下保持分段错误。您使用的汇编程序是什么?您是如何运行代码的?有一条
cmp
指令,它将省去您在
sub
之后重新加载原始代码的麻烦。它设置与
sub
相同的标志,但不写入目标寄存器。由于间隔都是相同的,您还可以检查>71,然后将该数字映射到查找表索引(减去71除以5,然后钳制最大索引)