汇编数组绝对值
Posted
技术标签:
【中文标题】汇编数组绝对值【英文标题】:Assembly array absolute value 【发布时间】:2017-06-27 15:08:01 【问题描述】:我正在尝试编写汇编程序,它在寄存器 ebx 中接收参数,并在寄存器 eax 中返回参数的绝对值,然后在循环中调用该程序以访问给定数组的每个元素并获取它们的绝对值价值观。当我运行代码时,我从汇编程序收到以下段错误。
Assembling: abs.asm
abs.asm(3) : error A2034: must be in segment block : ABS
abs.asm(4) : error A2034: must be in segment block : $$$00001
abs.asm(4) : error A2034: must be in segment block
abs.asm(5) : error A2034: must be in segment block : $$$00001
abs.asm(5) : error A2034: must be in segment block
abs.asm(6) : error A2034: must be in segment block : $$$00001
abs.asm(6) : error A2034: must be in segment block
abs.asm(7) : error A2034: must be in segment block : $$$00001
abs.asm(7) : error A2034: must be in segment block
abs.asm(8) : error A2034: must be in segment block
abs.asm(9) : error A2034: must be in segment block : $$$00001
abs.asm(9) : error A2034: must be in segment block
abs.asm(10) : error A2034: must be in segment block
abs.asm(11) : fatal error A1010: unmatched block nesting : ABS
INCLUDE Irvine32.inc
ABS PROC
cmp ebx,0 ;compare ebx to zero
jle L1 ;Jump if less then or equal to zero
mov eax, ebx ;copy ebx to eax, means eax is positive
Jmp L2 ;jumps to label return
L1:neg ebx ;negates ebx
mov eax,ebx ;copy neagted ebx to eax
L2:ret
ABS ENDP
.DATA
arrayW SDWORD 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20
.CODE
main PROC
mov esi, OFFSET arrayW ;address of arary
mov ecx, LENGTHOF arrayW ;loop counter
top:
mov ebx,[esi] ; move first element of arrayw to ebx
call ABS ; calls procedure abs and gets the eax value
call Writeint ; displays eax
add esi, TYPE arrayW ;points next element in array
loop top ;loop goes to the top
call clrscr
exit
main ENDP
END main
【问题讨论】:
ABS
是一个过程,这意味着它是代码。因此,它应该在.CODE
部分下。 (此外,值得一提的是,还有其他几种编写 ABS
的方法会大大更有效率。但我想这只是一个关于分支的学习练习。)
是的,你是对的。这是公正和分支的练习。它现在正在工作。谢谢
您在运行代码时没有遇到错误,但是当您尝试编译/组装它时,这些是编译时错误。汇编中的运行时错误看起来有点不同......基本上任何随机效应都是可能的,或者至少是,现代操作系统对随机机器代码执行非常坚固,但是对于早期的计算机,汇编代码中的任何错误通常会以重新启动而结束整台机器(丢失您之前在某些外部存储设备上未保存的任何进度)。
【参考方案1】:
我需要将ABS
过程移到.CODE
部分下:
INCLUDE Irvine32.inc
.DATA
arrayW SDWORD 3, -2, 5, 7, 2, 9, -11, 32, -19, 18, 17, 15, -5, 2, 3, 1, -21, 27,-29, 20
.CODE
ABS PROC
cmp ebx,0 ;compare ebx to zero
jle L1 ;Jump if less then or equal to zero
mov eax, ebx ;copy ebx to eax, means eax is positive
jmp L2 ;jumps to label return
L1:neg ebx ;negates ebx
mov eax,ebx ;copy neagted ebx to eax
L2:ret
ABS ENDP
main PROC
mov esi, OFFSET arrayW ;address of arary
mov ecx, LENGTHOF arrayW ;loop counter
top:
mov ebx,[esi] ;move first element of arrayw to ebx
call ABS ;calls procedure abs and gets the eax value
call Writeint ;displays eax
call Crlf
add esi, TYPE arrayW ;points next element in array
loop top ;loop goes to the top
exit
main ENDP
END main
【讨论】:
以上是关于汇编数组绝对值的主要内容,如果未能解决你的问题,请参考以下文章