簡體   English   中英

匯編語言x86 Irvine32

[英]Assembly Language x86 Irvine32

我是匯編語言的新手,所以我想弄清楚這個程序。 只想知道我是否對該程序感興趣。 我該如何糾正這個程序?

編寫一個循環,計算字節數組中所有元素的總和。 打印結果。 一些提示:將數組的大小加載到適當的寄存器中。 加載數組當前元素的偏移量,並在循環的每次迭代中相應地更改它。

這是我到目前為止的內容:

INCLUDE Irvine32.inc

.data
  val1 BYTE 1,2,3
  counter = 0
.code

main PROC

  mov ax, 0

  mov ax, (LENGTHOF val1)
  mov ax, OFFSET Counter
  movzx ecx,ax
L1:
  add eax, val1[ecx]
  inc eax

loop L1

  Call WriteDec

exit
 END PROC
   end main

您的代碼中有幾個錯誤:按照以下順序,您反復設置了ax,這幾乎沒有用:

mov ax, 0                          ; you set ax to 0
mov ax, (LENGTHOF val1)            ; you set ax to 3
mov ax, OFFSET Counter             ; you set ax to an address (and try to use a 16-bit register for a 32-bit address

然后您將此偏移量添加到

movzx ecx,ax
L1:
  add eax, val1[ecx]               ; add offset(val1)[offset(Counter)] to offset(Counter)

可以肯定的是,這將給您帶來內存錯誤,因為該地址可能在任何地方。 然后用

inc eax                            ; you probably confused this with a counter/index register

然后,您在movzx ecx, ax使用該偏移量在ECX中使用它,並將其作為LOOP指令中ECX中的索引

loop L1                            ; decrements ECX and loops if ECX != 0

修復所有這些錯誤后,代碼可能如下所示:

INCLUDE Irvine32.inc    
.data
  val1 BYTE 1,2,3
  counter = 0
.code
main:    
  xor eax, eax                     ; 32-bit register for the sum
  xor edx, edx                     ; 32-bit register for the counter/index
  mov ecx, LENGTHOF val1           ; number of entries in fixed size array in ECX as maximum limit
L1:
  movsx ebx, byte ptr val1[edx]    ; extend BYTE at offset val1 + EDX to 32-bit 
  add eax, ebx                     ; add extended BYTE value to accumulator
  inc edx                          ; increase index in EDX
loop L1                            ; decreases ECX and jumps if not zero

  Call WriteDec                    ; I assume this prints EAX
  exit
end main

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM