Printing out a number in assembly language?

后端 未结 10 1919
感动是毒
感动是毒 2020-12-03 04:39
mov al,10
add al,15

How do I print the value of \'al\'?

相关标签:
10条回答
  • 2020-12-03 05:10

    Call WinAPI function (if u are developing win-application)

    0 讨论(0)
  • 2020-12-03 05:14

    Have you tried int 21h service 2? DL is the character to print.

    mov dl,'A' ; print 'A'
    mov ah,2
    int 21h
    

    To print the integer value, you'll have to write a loop to decompose the integer to individual characters. If you're okay with printing the value in hex, this is pretty trivial.

    If you can't rely on DOS services, you might also be able to use the BIOS int 10h with AL set to 0Eh or 0Ah.

    0 讨论(0)
  • 2020-12-03 05:14
    PRINT_SUM PROC NEAR
     CMP AL, 0
     JNE PRINT_AX
     PUSH AX
     MOV AL, '0'
     MOV AH, 0EH
     INT 10H
     POP AX
     RET 
        PRINT_AX:    
     PUSHA
     MOV AH, 0
     CMP AX, 0
     JE PN_DONE
     MOV DL, 10
     DIV DL    
     CALL PRINT_AX
     MOV AL, AH
     ADD AL, 30H
     MOV AH, 0EH
     INT 10H    
        PN_DONE:
     POPA  
     RET  
    PRINT_SUM ENDP
    
    0 讨论(0)
  • 2020-12-03 05:14

    AH = 09 DS:DX = pointer to string ending in "$"

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

    ref: http://stanislavs.org/helppc/int_21-9.html


    .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
    
    0 讨论(0)
  • 2020-12-03 05:14

    Assuming you are writing a bootloader or other application that has access to the BIOS, here is a rough sketch of what you can do:

    • Isolate the first digit of the hex byte
    • If it is greater than 9 (i.e. 0x0A to 0x0F), subtract 10 from it (scaling it down to 0 to 5), and add 'A' (0x41).
    • If it is less than or equal to 9 (i.e. 0x00 to 0x09), add '0' to it.
    • Repeat this with the next hex digit.

    Here is my implementation of this:

    ; 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   
    
    0 讨论(0)
  • 2020-12-03 05:17
    ;        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
    
    0 讨论(0)
提交回复
热议问题