I’m brand new to assembly and I’m trying to write a program that reads a character and prints its ASCII code in hex. I’m using NASM on Linux, but I keep getting errors. Here’s my code:
section .data
char_prompt db "Please input a character: ",0
out_msg1 db "The ASCII code of '",0
out_msg2 db "' in hex is ",0
query_msg db "Do you want to quit (Y/N): ",0
hex_table db "0123456789ABCDEF",0
section .bss
; no variables needed
section .text
global _start
_start:
; prompt for character
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, char_prompt
mov edx, 24
int 0x80
; read character
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
lea ecx, [esp-1] ; temporary buffer on stack
mov edx, 1
int 0x80
mov al, [ecx] ; get the character
; output first message
mov eax, 4
mov ebx, 1
mov ecx, out_msg1
mov edx, 18
int 0x80
; output the character itself
mov eax, 4
mov ebx, 1
lea ecx, [esp-1] ; reuse buffer with the character
mov edx, 1
int 0x80
; output second message
mov eax, 4
mov ebx, 1
mov ecx, out_msg2
mov edx, 15
int 0x80
; convert to hex using xlatb
mov ah, al ; save character in ah
mov ebx, hex_table ; load table address
shr al, 4 ; get high nibble
xlatb ; replace al with hex digit
; output high nibble
push eax
mov eax, 4
mov ebx, 1
lea ecx, [esp] ; address of the digit on stack
mov edx, 1
int 0x80
pop eax
mov al, ah ; restore character
and al, 0x0F ; get low nibble
xlatb
; output low nibble
push eax
mov eax, 4
mov ebx, 1
lea ecx, [esp]
mov edx, 1
int 0x80
pop eax
; newline
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
; query to quit
mov eax, 4
mov ebx, 1
mov ecx, query_msg
mov edx, 20
int 0x80
; read reply
mov eax, 3
mov ebx, 0
lea ecx, [esp-1]
mov edx, 1
int 0x80
mov al, [ecx]
cmp al, 'Y'
jne _start ; if not 'Y', read another character
; exit
mov eax, 1
xor ebx, ebx
int 0x80
section .data
newline db 0x0A
But I’m getting an error: “error: invalid combination of opcode and operands” on the mov ebx, hex_table line. Can anyone help? The original code I found used a different syntax and I’m not sure what’s wrong.
