1

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
bashbin
  • 415
  • 1
  • 7
  • 21
  • So your question is how to convert the integer `11` to the string `"11"`? – Some programmer dude May 31 '17 at 08:59
  • @Someprogrammerdude The thing is that I'm not getting INTEGER 11 but ASCII 0Bh. It's printing out '♂' symbol which is the 11th symbol in ASCII table – bashbin May 31 '17 at 09:00
  • The length of the string `"Hello world"` is *eleven* characters (bytes). `11` in decimal is `0b` in hex, it's the same. What you need to do is to convert the integer value `11` to a *string* (i.e. the character `'1'` twice) and then print the string. Or if you're adamant it has to be printed as hex (why?) then convert the number `11` to the string `"0b"` and then print that string. The important part is the conversion to a string, now you have to decide if you want the string to contain the decimal or hexadecimal number. Either way, both have plenty of examples if you just search a little. – Some programmer dude May 31 '17 at 09:05
  • @Someprogrammerdude Values in registers are stored as hexadecimal values. I need to convert from hexadecimal (0Bh) to integer (11) and then print this integer. I don't need to output it as string, but if it's a must then yeah, 3 conversions? Anyway the focus is to print AL as a decimal value rather then hex (which it's getting the ascii value (the symbol)). Anyway, thanks! – bashbin May 31 '17 at 09:09
  • 3
    You must be careful to tell *numbers* from *numerals*. You have a number in a register, a number has no base - it is not in binary, hex or decimal - those are just the ways you *think* of it. A numeral is a representation of a number, only one conversion is needed once you have chosen the format of the numeral (hex, bin, Roman, Hebrew, ...). In the DOC there should still be examples you can learn from. – Margaret Bloom May 31 '17 at 09:13
  • 1
    No, values in registers are stored in *binary*. But they are *presented* in hexadecimal notation. On a binary computer *everything* is stored in binary, it's just presented differently. – Some programmer dude May 31 '17 at 09:16
  • 1
    you need a function to convert the content of CL to it's ascii representation. google for "x86 convert int to ascii" will surely find a lot of matches – Tommylee2k May 31 '17 at 09:57
  • Possible duplicate of [How to print a number in assembly NASM?](https://stackoverflow.com/questions/8194141/how-to-print-a-number-in-assembly-nasm) – David Hoelzer May 31 '17 at 11:02

1 Answers1

3
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
Fifoernik
  • 9,779
  • 1
  • 21
  • 27