如何打印整数,而不是ascii值?

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

我想循环添加五个数字并打印该总和。这是我的代码。

format MZ
stack sth:256
entry codeseg: main

segment sdat use16
;ds segment x
tabl db 1,2,3,4,10


segment sth use16
db 256 dup (?)

segment codeseg use16

main:
mov ax, sdat
mov ds, ax

;mov ax, sth
;mov ss, ax
;mov sp, 256

xor ax,ax
mov bx,tabl
mov cx,5
add:
        add ax,[bx]
        inc bx
        loop add

mov dx, ax
mov ah, 02h
int 21h


mov ax, 4c00h
int 21h
ret

我不知道我在做什么错,我想打印出总和,而不是ascii的值。

assembly dos x86-16 fasm
1个回答
0
投票

您必须将AX寄存器的内容转换为可以由操作系统显示的ASCII字符串。除其他更改外,我已相应更新了您的代码。现在您的挑战是解决所有问题;-)

format MZ
stack 256
entry codeseg:main

segment dataseg use16
tabl db 1,2,3,4,10
outstr db 8 dup ('$')

segment codeseg use16

main:
mov ax, dataseg
mov ds, ax
mov es, ax

xor ax,ax
mov bx,tabl
mov cx,5
addd:
    add al,[bx]
    adc ah, 0
    inc bx
    loop addd

lea di, [outstr]
call ax2dec

lea dx, [outstr]
mov ah, 09h
int 21h

mov ax, 4c00h
int 21h

ax2dec:                                 ; Args: AX:number ES:DI: pointer to string
    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 [es:di], '$'               ; Termination character for 'int 21h fn 09h'
    ret
© www.soinside.com 2019 - 2024. All rights reserved.