I'm trying to learn nasm, following this tutorial , I have written this code
section .text
global _start
_start:
mov al, 1ah ; 0001 1010
mov bl, 40h ; 0100 0000
or al, bl ; 0101 1010 ==> 'Z'
add al, byte '0' ; convert from decimal to ascii
mov [result], al
mov eax, 4 ;syscall (write)
mov ebx, 1 ;file descirptor
mov ecx, result ;message to write
mov edx, 1 ;message length
int 0x80 ;call kernell
jmp outprog
outprog:
mov eax, 1
int 0x80
segment .bss
result resb 1
the output of nasm -f elf hello.asm ; ld -m elf_i386 -s -o hello hello.o; ./hello
is a weird character �%
where it had to print 'z'
am I missing soemthing?
CodePudding user response:
If the comment on or al, bl ; 0101 1010 ==> 'Z'
already says that this is a character, then it's not clear why you still add something to it.
Your addition add al, byte '0'
adds 48 to the ASCII code for 'Z':
0101 1010 90 'Z'
0011 0000 48 '0'
---------
1000 1010 138 'è' in codepage 437
The addition of byte '0'
is only needed to convert values in the range 0 to 9 into characters in the range "0" to "9".
it had to print 'z'
To convert the uppercase 'Z' into the lowercase 'z' that you seem to expect, the addition would need to be 32.
mov al, 1ah ; 0001 1010
mov bl, 40h ; 0100 0000
or al, bl ; 0101 1010 ==> 'Z'
add al, 32 ; 0111 1010 ==> 'z'