如何更改数组中的最后一个索引值?

问题描述 投票:0回答:2

我们开始使用TASM学习Assembly。我的老师提出了一些要解决的问题,还有一个我不理解的问题。我尝试搜索堆栈溢出,但是这完全没有帮助。

所以问题是:

1)a声明一个2字节大小的数组,包含8个元素,起始值为6。

我对此的回答:ar dw 8 dup(6)

1] b编写将最后一个索引值更改为7的命令。

因为我们刚开始上课,所以我不知道如何解决这个问题(1b)。如果您能帮助我解决这个问题,我将非常高兴。

assembly tasm
2个回答
0
投票

这是您的TASM程序模板:

.MODEL small
.486
.STACK 1000h

.DATA
    ar      DW 8 DUP (6)
    decstr  DB 8 ('$')

.CODE
main PROC
    mov ax, @DATA                       ; Initialize DS - Don't forget it!
    mov ds, ax

; You might count the array from 1 to 8, but the computer counts from 0 to 7.
; So, decrement the wished index by 1: (LENGTH arr - 1)
; Since a WORD consumes 2 bytes, the index has to be multiplied by this size,
; so that the computer can point into the array at the right address.
; The size of a WORD (2 bytes) can be determined by the directive TYPE
LASTINDEX =  (LENGTH ar - 1) * TYPE ar

    mov [ar + LASTINDEX],  7

; Print the decimal numbers of the array to the console
    lea si, [ar]                        ; SI gets the address of ar
    mov cx, 8                           ; Counter - do the loop eight times
    L1:
    mov ax, [si]                        ; AX gets the value of the element
    lea di, [decstr]                    ; DI gets the address of decstr
    call ax2dec
    mov WORD PTR [di], '$ '             ; Insert a space into the string
    mov ah, 09h                         ; DOS function: print string
    lea dx, [decstr]                    ; DX gets the address of decstr
    int 21h                             ; Call DOS
    add si, 2                           ; Set the pointer to the next element
    loop L1                             ; Do it CX times

    mov ax, 4C00h                       ; DOS function:Exit with Code 0
    int 21h                             ; Call DOS
main ENDP

ax2dec PROC STDCALL USES ax bx cx dx es ; Args: AX - number; DI - pointer to string
    push ds                             ; Initialize ES for STOSB
    pop es
    mov bx, 10                          ; Base 10 -> divisor
    xor cx, cx                          ; CX=0 (number of digits)

  Loop_1:
    xor dx, dx                          ; Clear DX for division
    div bx                              ; AX = DX:AX / BX   Remainder DX
    push dx                             ; Push remainder for LIFO in Loop_2
    inc cx                              ; inc cl = 2 bytes, inc cx = 1 byte
    test ax, ax                         ; AX = 0?
    jnz Loop_1                          ; No: once more

  Loop_2:
    pop ax                              ; Get back pushed digits
    or al, 00110000b                    ; Conversion to ASCII
    stosb                               ; Store only AL to [ES:DI] (DI is a pointer to a string)
    loop Loop_2                         ; Until there are no digits left
    mov byte ptr es:[di], '$'           ; Termination character for 'int 21h fn 09h'
    ret
ax2dec ENDP                             ; Ret: DI - pointer to terminating '$'

END main                                ; Directive to stop the compiler.

如果您的老师的例子不同,请问他们为什么! TASM很古老,但其老师却更古老;-)


0
投票

数组大小自然是固定的。

在您的情况下,数组有8个元素,每个元素的大小为2个字节。

因此,您尝试访问的地址距该地址14个字节。

LEA SI,AR+14
MOV AX,1000H
MOV [SI],AX

我还没有亲自测试过此代码,所以不确定是否可以将1000H作为2个字节的数据直接传递给[SI]。

© www.soinside.com 2019 - 2024. All rights reserved.