bashbin
bashbin

Reputation: 414

emu8086 - How to get CHAR from ASCII code

I want to print the length of the string 'Hello World', im getting the value (0Bh which is 11, but the interrupt is printing the ascii character which is ♂ and not 11)

    org 100h

LEA SI, msg
MOV CL, 0

CALL printo


printo PROC
next_char:

    CMP b.[SI],0
    JE stop

    MOV AL,[SI]

    MOV AH, 0Eh
    INT 10h

    INC SI
    INC CL

    JMP next_char

printo ENDP

stop:

MOV AL,CL  ; CL is 0B (11 characters from 'Hello World')
MOV AH, 0Eh
INT 10h ; but it's printing a symbol which has a ascii code of 0B

ret

msg db 'Hello World',0
END

Upvotes: 1

Views: 2184

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

MOV  AL,CL  ; CL is 0B (11 characters from 'Hello World')

With the length in AL it's easy to represent it in decimal notation.
The AAM instruction will first divide the AL register by 10 and then leave the quotient in AH and the remainder in AL.
Please take note that this solution works for all lengths from 0 to 99.

aam
add  ax, "00" ;Turn into text characters
push ax
mov  al, ah
mov  ah, 0Eh  ;Display tenths
int  10h
pop  ax
mov  ah, 0Eh  ;Display units
int  10h

In the event that the number involved is smaller than 10, it would be ugly to actually have a zero displayed in front of the single digit number. Then just add the next to the code:

 aam
 add  ax, "00" ;Turn into text characters
 cmp  ah, "0"
 je   Skip
 push ax
 mov  al, ah
 mov  ah, 0Eh  ;Display tenths
 int  10h
 pop  ax
Skip:
 mov  ah, 0Eh  ;Display units
 int  10h

Upvotes: 3

Related Questions