如何在DOS x86程序集中打印数组?

时间:2014-05-15 17:21:49

标签: arrays assembly printing x86 dos

我有这个代码来复制数组" NUMBERS"到" DEST"没有数字会重复(在这种情况下应该是:1,2,5,4,7) 代码有效但现在我需要打印数组" DEST"。我怎么能这样做?

data segment
NUMBERS db 1,2,1,1,1,5,5,4,7,7
DEST dt ?
data ends

code segment
assume ds:data, cs:code
start: mov ax, data
       mov ds, ax
                   mov ax, 0a0ah
                   mov di, offset NUMBERS
                   mov bx, 0h

             loop2:mov cl, [di]
                   mov si, offset DEST
                   mov ch, [si]
             loop1:cmp ch, cl
                   je dontadd
                   inc si
                   mov ch, [si]
                   dec ah
                   jnz loop1
                   mov si, offset DEST
                   add si, bx
                   inc bx
                   mov [si], cl
           dontadd:mov ah, 0ah
                   inc di
                   dec al
                   jnz loop2

       mov ah, 4ch
       int 21h
code ends
end start

2 个答案:

答案 0 :(得分:0)

您可以使用INT 21h, AH=02h将单个字符打印到STDOUT。

 MOV CX,10
 MOV SI,OFFSET DEST
print:
 MOV DL,[si]

 OR DL,DL
 JE done

 ADD DL,'0'  ; <-- Convert numeric value in DL into ASCII code
 MOV AH,02h
 INT 21h

 MOV DL,' '  ; Throw in a space to make things pretty
 INT 21h

 INC SI
 LOOP print
done:

如果NUMBERS中的元素的值大于9,那么您需要将数值更精细地转换为ASCII。

答案 1 :(得分:0)

首先将数字转换为字符串然后打印它们。您可以使用这两个宏来打印数字:

printstr macro str
push ax
push dx
lea dx,str
mov ah,9
int 21h
pop dx
pop cx
endm

printnum macro n
local o,w,s,n5,lb1,lb2,lb3
pusha
push si
push di
jmp w
s db 7 dup('$')
w:
mov si,0
mov cx,7
o:mov s[si],'$'
inc si
loop o
mov si,0
xor ax,ax
mov ax,n
xor dx,dx
cmp ax,0
jge n5
mov bl,'-'
mov s[si],bl
inc si
neg ax
n5:mov cx,10
div cx
or dl,30h
mov s[si],dl
xor dx,dx
inc si
cmp ax,0
jne n5
mov si,0
cmp s[si],'-'
jne lb1
inc si
lb1:mov di,si
lb3:cmp s[si],'$'
je lb2
mov al,s[si]
push ax
inc si
jmp lb3
lb2:pop ax
mov s[di],al
inc di
cmp di,si
jl lb2
printstr s
pop di
pop si 
popa
endm