简体   繁体   English

有条件的循环MASM x86程序集

[英]Conditional in Loop MASM x86 Assembly

I am using the Irvine library. 我正在使用尔湾图书馆。

I want to iterate through a DWORD array while checking if each value is in the range of j and k. 我想在检查每个值是否在j和k范围内时遍历DWORD数组。 The code I have does not work currently. 我拥有的代码目前无法正常工作。 Here is what I have: 这是我所拥有的:

INCLUDE Irvine32.inc
COMMENT !
.386
.model flat, stdcall
.stack 4096

ExitProcess PROTO, code:DWORD
DumpRegs PROTO
!

.data
myArr DWORD 1h,2h,3h,4h
prompt BYTE "Enter the value for j and k: ", 0
counter BYTE ?

.code
MAIN PROC
  mov eax, 0              ;    sum

  ; Get user vals for j and k
  mov edx, OFFSET prompt
  CALL WriteString
  CALL ReadInt
  mov ebx, eax

  CALL ReadInt
  mov edx, eax



  CALL sumArr             ; Call #1  *****

  mov ebx, 5              ;   j = 5
  mov edx, 8              ;   k = 8

  CALL sumArr             ; Call #2  *****


Main endP



sumArr PROC USES esi ecx edx ebx
    mov counter, LENGTHOF myArr
    mov esi, OFFSET myArr   ;    location pointer
    mov ecx, LENGTHOF myArr ;    size


    getArr:
      cmp ebx, [esi]
      jae aboveEqual
      add esi, TYPE myArr


    aboveEqual:
      cmp edx, [esi]
      jbe inRange

    inRange:
      add eax, [esi]

      LOOP getArr
    CALL DumpRegs
    RET
sumArr ENDP

I want to be able to do a comparison for each element in the array. 我希望能够对数组中的每个元素进行比较。 How can I do this? 我怎样才能做到这一点?

You forgot to zero EAX in SUMARR. 您忘记了SUMARR中的EAX为零。
Both cmp ebx,[esi] and cmp edx,[esi] must have source and destination operands switched place. cmp ebx,[esi]cmp edx,[esi]必须将源和目标操作数进行交换。
Move add esi, TYPE myArr right before the LOOP instruction. add esi, TYPE myArr移动到LOOP指令之前。
If the conditional jumps are not taken then unconditionally jump to the add esi, TYPE myArr instruction. 如果不执行条件跳转,则无条件跳转到add esi, TYPE myArr指令。

In mov ecx, LENGTHOF myArr will the immediate be 4 or will it be 16? mov ecx, LENGTHOF myArr的立即数是4还是16?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM