x86 Irvine32:汇编语言 - 获取最后一个数组

时间:2016-05-06 13:41:13

标签: arrays assembly x86 irvine32

我正在试图找出这个程序。只是想知道我是否与该计划有关。我该如何更正此程序?任何帮助表示赞赏。谢谢。

“编写一个包含单词数组的汇编语言程序。程序将数组的最后一个元素加载到一个适当大小的寄存器中并打印出来。(不要硬编码最后一个元素的索引。)”

INCLUDE Irvine32.inc    
.data
  val1 word 1,2,3,4,5,6
  val2 = ($-val1)/2  ; This is hardcoding
.code
main PROC        
  mov ax, 0
  mov ax, val1[val2]

  Call WriteDec
  Call DumpRegs
 exit
main ENDP
END main

1 个答案:

答案 0 :(得分:0)

如果你不能使用val2 = ($-val1)/2,一个选择是为数组选择一些终止字符,例如'$',并循环直到它找到:

INCLUDE Irvine32.inc    
.data
  val1 word 1,2,3,4,5,6,'$'                ;ARRAY WITH TERMINATING CHARACTER.
  val2 = ($-val1)/2  ; This is hardcoding
.code
main PROC        
  ;mov ax, 0
  ;mov ax, val1[val2]

  mov si, offset val1    ;SI POINTS TO VAL1.
  mov ax, '$'            ;TERMINATING CHARACTER.
repeat:
  cmp [ si ], ax
  je  dollar_found       ;IF [ SI ] == '$'
  add si, 2              ;NEXT WORD IN ARRAY.
  jmp repeat

dollar_found:  
  sub si, 2              ;PREVIOUS WORD.
  mov ax, [ si ]         ;FINAL WORD!

  Call WriteDec
  Call DumpRegs
 exit
main ENDP
END main