Assembly 用汇编语言打印出一个数字?

Assembly 用汇编语言打印出一个数字?,assembly,x86,real-mode,Assembly,X86,Real Mode,如何打印“al”的值?调用WinAPI函数(如果您正在开发win应用程序)汇编语言无法直接打印任何内容。您的汇编器可能附带或不附带提供这种功能的库,否则您必须自己编写,这将是一个相当复杂的函数。你还必须决定在哪里打印东西——在窗口中,在打印机上?在汇编程序中,这些都不是为您完成的。您尝试过吗DL是要打印的字符 mov al,10 add al,15 要打印整数值,必须编写一个循环,将整数值分解为单个字符。如果您可以用十六进制打印值,这是非常简单的 如果您不能依赖DOS服务,您还可以使用设置为0

如何打印“al”的值?

调用WinAPI函数(如果您正在开发win应用程序)

汇编语言无法直接打印任何内容。您的汇编器可能附带或不附带提供这种功能的库,否则您必须自己编写,这将是一个相当复杂的函数。你还必须决定在哪里打印东西——在窗口中,在打印机上?在汇编程序中,这些都不是为您完成的。

您尝试过吗
DL
是要打印的字符

mov al,10
add al,15
要打印整数值,必须编写一个循环,将整数值分解为单个字符。如果您可以用十六进制打印值,这是非常简单的


如果您不能依赖DOS服务,您还可以使用设置为
0Eh
0Ah

AL
来调用Win32 API的MessageBoxA,尽管Win16是否支持该特定方法由其他人来回答。

DOS打印存储在EAX中的32位值,并使用十六进制输出(对于80386+)
mov dl,'A' ; print 'A'
mov ah,2
int 21h
(在64位操作系统上使用DOSBOX)

不使用软件中断,直接将可选字符串输出到videobuffer:

.code
    mov ax,@DATA        ; get the address of the data segment
    mov ds,ax           ; store the address in the data segment register
;-----------------------
    mov eax,0FFFFFFFFh  ; 32 bit value (0 - FFFFFFFF) for example
;-----------------------
; convert the value in EAX to hexadecimal ASCIIs
;-----------------------
    mov di,OFFSET ASCII ; get the offset address
    mov cl,8            ; number of ASCII
P1: rol eax,4           ; 1 Nibble (start with highest byte)
    mov bl,al
    and bl,0Fh          ; only low-Nibble
    add bl,30h          ; convert to ASCII
    cmp bl,39h          ; above 9?
    jna short P2
    add bl,7            ; "A" to "F"
P2: mov [di],bl         ; store ASCII in buffer
    inc di              ; increase target address
    dec cl              ; decrease loop counter
    jnz P1              ; jump if cl is not equal 0 (zeroflag is not set)
;-----------------------
; Print string
;-----------------------
    mov dx,OFFSET ASCII ; DOS 1+ WRITE STRING TO STANDARD OUTPUT
    mov ah,9            ; DS:DX->'$'-terminated string
    int 21h             ; maybe redirected under DOS 2+ for output to file
                        ; (using pipe character">") or output to printer

  ; terminate program...

.data
ASCII DB "00000000",0Dh,0Ah,"$" ; buffer for ASCII string
AH=09 DS:DX=指向以“$”结尾的字符串的指针

参考:



假设您正在编写引导加载程序或具有BIOS访问权限的其他应用程序,下面是您可以执行的操作的大致示意图:

  • 隔离十六进制字节的第一个数字
  • 如果大于9(即0x0A到0x0F),则从中减去10(按比例缩小到0到5),然后添加“A”(0x41)
  • 如果它小于或等于9(即0x00到0x09),则将“0”添加到它
  • 对下一个十六进制数字重复此操作
以下是我对这一点的实施:

.data  

string db 2 dup(' ')

.code  
mov ax,@data  
mov ds,ax

mov al,10  
add al,15  
mov si,offset string+1  
mov bl,10  
div bl  
add ah,48  
mov [si],ah  
dec si  
div bl  
add ah,48  
mov [si],ah  

mov ah,9  
mov dx,string  
int 21h

您需要指定您的机器类型和操作系统,因为I/O通常非常依赖于平台。他这样做了,它有一个“win16”标记。win16真的吗?我们仍然在使用Windows 3.1吗?如果您使用asm或C编写Windows应用程序(即使使用)。。。然后,函数将是WinAPI函数。这就像对说“我病了”的人回答“你应该去看医生”。真棒的帮助。。我找了很长时间。。到处都是mov-ah,09用于打印数组,而不是mov-ah,02用于打印单个值。糟糕的格式。被否决的理由是,大量空白、无缩进、无注释几乎无法阅读。被否决的理由是,这根本无法回答问题,而是打印出一颗心。
;        good example of      unlimited num print

.model small

.stack 100h

.data

number word 6432

string db 10 dup('$')

.code


main proc

mov ax,@data 

mov ds,ax



mov ax,number

mov bx ,10

mov cx,0

l1:

mov dx,0

div bx

add dx,48

push dx

inc cx

cmp ax,0

jne l1


mov bx ,offset string 

l2:

pop dx           

mov [bx],dx

inc bx



loop l2



mov ah,09

mov dx,offset string

int 21h
mov ax,4c00h

int 21h


main endp

end main
.code
    mov ax,@DATA        ; get the address of the data segment
    mov ds,ax           ; store the address in the data segment register
;-----------------------
    mov eax,0FFFFFFFFh  ; 32 bit value (0 - FFFFFFFF) for example
;-----------------------
; convert the value in EAX to hexadecimal ASCIIs
;-----------------------
    mov di,OFFSET ASCII ; get the offset address
    mov cl,8            ; number of ASCII
P1: rol eax,4           ; 1 Nibble (start with highest byte)
    mov bl,al
    and bl,0Fh          ; only low-Nibble
    add bl,30h          ; convert to ASCII
    cmp bl,39h          ; above 9?
    jna short P2
    add bl,7            ; "A" to "F"
P2: mov [di],bl         ; store ASCII in buffer
    inc di              ; increase target address
    dec cl              ; decrease loop counter
    jnz P1              ; jump if cl is not equal 0 (zeroflag is not set)
;-----------------------
; Print string
;-----------------------
    mov dx,OFFSET ASCII ; DOS 1+ WRITE STRING TO STANDARD OUTPUT
    mov ah,9            ; DS:DX->'$'-terminated string
    int 21h             ; maybe redirected under DOS 2+ for output to file
                        ; (using pipe character">") or output to printer

  ; terminate program...

.data
ASCII DB "00000000",0Dh,0Ah,"$" ; buffer for ASCII string
;-----------------------
; Print string
;-----------------------
    mov ax,0B800h       ; segment address of textmode video buffer
    mov es,ax           ; store address in extra segment register

    mov si,OFFSET ASCII ; get the offset address of the string

; using a fixed target address for example (screen page 0)
; Position`on screen = (Line_number*80*2) + (Row_number*2)

    mov di,(10*80*2)+(10*2)
    mov cl,8            ; number of ASCII
    cld                 ; clear direction flag

P3: lodsb  ; get the ASCII from the address in DS:SI + increase si
    stosb  ; write ASCII directly to the screen using ES:DI + increase di
    inc di ; step over attribut byte
    dec cl ; decrease counter
    jnz P3 ; repeat (print only 8 ASCII, not used bytes are: 0Dh,0Ah,"$")

; Hint: this directly output to the screen do not touch or move the cursor
; but feel free to modify..
returns nothing


- outputs character string to STDOUT up to "$"
- backspace is treated as non-destructive
- if Ctrl-Break is detected, INT 23 is executed
.data  

string db 2 dup(' ')

.code  
mov ax,@data  
mov ds,ax

mov al,10  
add al,15  
mov si,offset string+1  
mov bl,10  
div bl  
add ah,48  
mov [si],ah  
dec si  
div bl  
add ah,48  
mov [si],ah  

mov ah,9  
mov dx,string  
int 21h
; Prints AL in hex.
printhexb:
    push ax
    shr al, 0x04
    call print_nibble
    pop ax
    and al, 0x0F
    call print_nibble
    ret
print_nibble:
    cmp al, 0x09
    jg .letter
    add al, 0x30
    mov ah, 0x0E
    int 0x10
    ret
.letter:
    add al, 0x37
    mov ah, 0x0E
    int 0x10
    ret