如何在程序集中显示结果

时间:2013-06-14 19:27:29

标签: assembly

我是学习集会的初学者。我在Windows 7 64bit下使用DOSBOX。我想知道如何在汇编中显示寄存器的值。例如,我编写了以下代码:

mov ax,20 ; ax will be 20
mov cx,10 ; cx will be 10
sub ax,cx ; ax will be 10 (ax-cx,20-10)

如何显示ax寄存器的内容(应该是10)?

1 个答案:

答案 0 :(得分:2)

您尚未指定目标操作系统或汇编程序,但如果我们假设DOS和NASM(不应该很难适应TASM / MASM):

org 100h
section .text

mov ax,123

mov byte [buffer+9],'$'     ; Insert string terminator
lea si,[buffer+9]

; create a string representation of the value in AX
mov bx,10
itoa:
    xor dx,dx       ; clear dx, since we'll divide dx:ax by bx
    div bx          ; ax = dx:ax / 10, dx = dx:ax % 10 
    add dl,'0'      ; add '0' to the remainder to get a character in the range '0'..'9'
    dec si
    mov [si],dl     ; store it in the buffer
    cmp ax,0
    jnz itoa

mov ah,9            ; print string
mov dx,si
int 21h

mov ax,4Ch          ; exit to DOS
int 21h

buffer: resb 10