-1

I can't get the following code to run. It compiles but just gives a blank output. Im trying to take the value of HEX_OUT, remove the trailing zero and then and it with the value of register dx so that the value of HEX_OUT is now the value of dx. Any help would be much appreciated.

Thanks,

Ash

; prints the value of DX as hex.
print_hex:
pusha

mov ax, HEX_OUT     ; ax = 0x00000
shr ax, 1       ; remove trailing 0
and ax, dx      ; and registers and store in ax

        ; TODO :manipulate chars at HEX_OUT to reflect DX

mov bx, ax          ; print the string pointed to      
call print_string   ; by BX

popa
ret

; global variables
HEX_OUT : db '0x0000',0
Community
  • 1
  • 1
  • 1
    What error messages are you getting? What assembler are you using? Where is `print_string` defined? – lurker Jan 29 '14 at 17:16
  • Print string is defined in another .asm file, both are part of a 'master' file. It all compiles without error using nasm but when you run it, its just giving blank output – user3249855 Jan 29 '14 at 17:28
  • also, if i comment out the shr line and the and line, it displays 0x0000 ....so its got to be something to do with those two lines – user3249855 Jan 29 '14 at 17:30
  • Think about what you're doing in those lines. `mov ax,HEX_OUT` is putting the *address* of your string `0x0000` into `ax`. With the `shr` and `and` commented out, the next thing that happens is the string pointed to by `ax` is printed using `print_string`. If you uncomment those lines, you are taking the address of `0x0000` in `ax` and manipulating it (dividing by 2 and `and`-ing who-knows-what that's in `dx` with it) and getting some new address which points to... what? Who knows, but it's probably not good. You're manipulating the address in this case, not the value. – lurker Jan 29 '14 at 17:34

1 Answers1

0

HEX_OUT is not a binary number; it is the address of a string (that just happens to be a hexadecimal representation of a number). shr ax,1 does not manipulate strings; it does not shift characters. It manipulates binary numbers; it shifts bits.

The easiest way out is to avoid strings altogether:

HEX_OUT: dw 0000h    ; or whatever number you like

I hope you have a function like print_number at your disposal:

mov bx, ax           ; or whatever register is the input for that function
call print_number    ; or whatever is the name of that function

Because otherwise you'll have to write (or find) code to convert a binary number to its hexadecimal string representation, so that you can feed that to print_string.

Community
  • 1
  • 1
Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45