3

I just started learning assembly, so this is, without doubt, a stupid question, but I still can't figure it out. I'm trying to write a procedure that would print the given 16-bit word in hex.

I figured that I could divide it into 4-bit chunks by shifting left and right, and then use it as an index to copy symbol constant from prepared table to output variable. Here how my code looks right now:

; Print hex at AX print_hex: pusha mov cx, 0 ; iterator CX = 0 print_hex_loop: mov dx, bx ; DX = symbol index shl dx, cx ; Loose more significant digits << ERROR shr dx, 3 ; Loose less significant digits add dx, HEX_TABLE ; DX = table address add cx, HEX_OUT + 2 ; CX = output address mov byte [cx], [dx] ; HEX_OUT[cx] = HEX_TABLE[dx] << ERROR sub cx, HEX_OUT + 2 ; CX = iterator add cx, 1 ; increase iterator CX cmp cx, 4 ; if iterator CX less then 4 jb print_hex_loop ; repeat loop mov ax, HEX_OUT call print_string_loop popa ret HEX_TABLE: db '0123456789abcdef' HEX_OUT: db '0x0000', 0 

However, I get errors that I don't quite understand on shl and mov lines:

utility.asm:23: error: invalid combination of opcode and operands utility.asm:27: error: invalid combination of opcode and operands 

What am I doing wrong here?

2
  • It would be easier if you indicated which lines were 23 & 27. Commented Sep 29, 2014 at 15:55
  • @JamesCurran there are << ERROR signs at their ends Commented Sep 29, 2014 at 17:17

1 Answer 1

3
shl dx, cx ; Loose more significant digits << ERROR 

That should be cl, not cx

mov byte [cx], [dx] ; HEX_OUT[cx] = HEX_TABLE[dx] << ERROR 

Memory to memory operations are generally not supported by the CPU. You must do this in a two-step operation - load from memory location to register, then store from register to (other) memory location.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I was able to solve the first error, easily, but the second one confuses me. After a few experiments, I found out that mov ax, [bx] is absolutely correct, but mov ax, [dx] gives an invalid effective address error. Why? Aren't dx and bx equal?
It's been a long time since I coded in 16-bit land, but I seem to recall that not all registers can be used for indexing in that mode. You might want to try si rather than dx.
@golergka In 16-bit mode addresses are of form [displacement + bx|bp + si|di]. You can only use bx or bp for base and si or di for index.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.