The MASM Forum

Miscellaneous => 16 bit DOS Programming => Topic started by: endrodyp on June 15, 2019, 10:25:44 PM

Title: xlat, xlatb
Post by: endrodyp on June 15, 2019, 10:25:44 PM
Has anyone experienced This? When compiling with options .286 and otion expr16 the xlatb opcodes doesnt seem to work. I was trying to make an Word to hexadecimal translator using xlatb but had to find another way.

original:
;-------------------------------------------------------------------------------------------
; szam konvertalasa hex ascii kodokka , hextable    db "0123456789ABCDEF","$" a progi elejére
;
; AX= konvertalando szám
; di = puffer címe (7 bájt)
;-------------------------------------------------------------------------------------------
wtohex:
.data
wtohex_hextable   db "0123456789abcdef"

.code
push bx

lea bx, wtohex_hextable
push ax
and ax,0f000h
shr ax,12
xlat
mov byte ptr [di],al
pop ax
push ax
and ax, 00f00h
shr ax,8
xlat
mov byte ptr [di+1],al
pop ax
push ax
and ax, 000f0h
shr ax,4
xlat
mov byte ptr [di+2],al
pop ax
and ax, 0000fh
xlat
mov byte ptr [di+3],al
mov byte ptr [di+4],'h'
mov byte ptr [di+5],"%"
mov byte ptr [di+6],"%"

pop bx
ret
;-------------------------------------------------------------

modified:

;-------------------------------------------------------------------------------------------
; szam konvertalasa hex ascii kodokka , hextable    db "0123456789ABCDEF","$" a progi elejére
;
; AX= konvertalando szám
; di = puffer címe (7 bájt)
;-------------------------------------------------------------------------------------------
wtohex:

push bx

push ax
and ax,0f000h
shr ax,12
cmp al,10
jge letter1
add al, 48
jmp next
letter1:
add al, 65
next:
mov byte ptr [di],al
pop ax
push ax
and ax, 00f00h
shr ax,8
cmp al, 10
jge letter2
add al, 48
jmp next2
letter2:
add al, 65
next2:
mov byte ptr [di+1],al
pop ax
push ax
and ax, 000f0h
shr ax,4
cmp al, 10
jge letter3
add al, 48
jmp next3
letter3:
add al, 65
next3:
mov byte ptr [di+2],al
pop ax
push ax
and ax, 0000fh
cmp al, 10
jge letter4
add al, 48
jmp next4
letter4:
add al, 65
next4:
mov byte ptr [di+3],al
pop ax
mov byte ptr [di+4],'h'
mov byte ptr [di+5],"%"
mov byte ptr [di+6],"%"

pop bx
ret
;-------------------------------------------------------------
Title: Re: xlat, xlatb
Post by: jj2007 on June 15, 2019, 11:11:52 PM
What exactly doesn't work? These three instructions assemble just fine on my machine, using ML version 6.14 or UAsm:
  .186
  xlat table
  xlat [bx]
  xlatb

Which assembler are you using, which errors do you get?
Title: Re: xlat, xlatb
Post by: endrodyp on June 16, 2019, 12:03:28 AM
It seems to compile fine without errors, however the result is an empty string Masm ver. 5.12.
Title: Re: xlat, xlatb
Post by: jj2007 on June 16, 2019, 12:27:41 AM
You should at least use Masm 6.14, which comes with the Masm32 SDK (installation instructions (http://www.jj2007.eu/Masm32_Tips_Tricks_and_Traps.htm)); UAsm is much better (http://www.terraspace.co.uk/uasm.html#p2). Your code as 32-bit version works, although I am not sure whether the result 6364h%% pleases you:
include \masm32\include\masm32rt.inc  ; standard includes for a plain Masm32 project
.code
wtohex:

.data
wtohex_hextable   db "0123456789abcdef"
buffer db 1000 dup(?)

.code
push ebx
mov edi, offset buffer
lea ebx, wtohex_hextable
push eax

and eax,0f000h
shr eax,12
xlat
mov byte ptr [edi],al
pop eax
push eax
and eax, 00f00h
shr eax,8
xlat
mov byte ptr [edi+1],al
pop eax
push eax
and eax, 000f0h
shr eax,4
xlat
mov byte ptr [edi+2],al
pop eax
and eax, 0000fh
xlat
mov byte ptr [edi+3],al
mov byte ptr [edi+4],'h'
mov byte ptr [edi+5],"%"
mov byte ptr [edi+6],"%"

pop ebx
ret
start:
  mov eax, "abcd"
  call wtohex
  inkey offset buffer
  exit
end start

Title: Re: xlat, xlatb
Post by: aw27 on June 16, 2019, 12:55:55 AM
Try to post the whole program. Building for .286 is not the same as building for .386.
Title: Re: xlat, xlatb
Post by: endrodyp on June 16, 2019, 01:20:11 AM
Yes the point is that I can only use it in 16 bit environment. I, too, tested it in 32 bit environment it Works fine, which I cannot say to 16 bit DOS.
Title: Re: xlat, xlatb
Post by: sinsi on June 16, 2019, 09:27:33 AM
Have you initialised DS to point to the data segment? For a .com program DS=ES=CS=SS but an EXE you need to load DS.
Title: Re: xlat, xlatb
Post by: endrodyp on June 16, 2019, 04:54:42 PM
it is a .com program. Compiled with ml /AT filename.asm and link16 /TINY filename.obj.
Anyway Thanks guys I simply use the second algorythm for my project. No big deal.
Title: Re: xlat, xlatb
Post by: aw27 on June 16, 2019, 05:38:15 PM

;\masm32\bin\ml614 -c test.asm
;\masm32\bin\link16 test.obj, test.exe,0,0,nul.def
.286
.model small

.stack 100h

.data
wtohex_hextable   db "0123456789abcdef",0
myWord db "Word to Hex=$",0
buffer db 64 dup (0)
Sixteen db 16

.code

.startup
push di
push si
push bx
push es
mov ax, ds
mov es, ax

lea     dx, myWord
mov     ah,09
int     21h

mov si, offset myWord
mov di, offset buffer
mov bx, offset wtohex_hextable
mov cx, sizeof myWord
sub cx,3
next:
mov ah,0
lodsb
div Sixteen ;(result in AH:AL, not DX:AX)
xlatb
stosb
mov al, ah
xlatb
stosb
loop next

mov al, 'h'
stosb
mov al, '$'
stosb

lea     dx, buffer
mov     ah,09
int     21h

pop es
pop bx
pop si
pop di
.EXIT
end



Word to Hex=576f726420746f20486578h


PS: I don't think there is any MASM version 5.12, it exists a LINK with that version number.  :skrewy:
Title: Re: xlat, xlatb
Post by: jj2007 on June 16, 2019, 07:50:26 PM
Quote from: endrodyp on June 16, 2019, 04:54:42 PM
it is a .com program. Compiled with ml /AT filename.asm and link16 /TINY filename.obj.
Anyway Thanks guys I simply use the second algorythm for my project. No big deal.

You can use the first algo, it's fine. This works like a charm:
.186
.model tiny
.code
org 100h

start:
  mov ax, 0AB12h ; test string
  call wtohex
  ret

wtohex_hextable   db "0123456789abcdef"

wtohex:
sub sp, 100 ; create a buffer
mov di, sp
push bx
lea bx, wtohex_hextable
push ax
and ax,0f000h
shr ax,12
xlat
mov byte ptr [di],al
pop ax
push ax
and ax, 00f00h
shr ax,8
xlat
mov byte ptr [di+1],al
pop ax
push ax
and ax, 000f0h
shr ax,4
xlat
mov byte ptr [di+2],al
pop ax
and ax, 0000fh
xlat
mov byte ptr [di+3],al
mov byte ptr [di+4],'h'
mov byte ptr [di+5],"$"
mov byte ptr [di+6],"%"
pop bx
mov ah, 09h ; write string to STDOUT
mov dx, di
int 21h
add sp, 100
ret

end start
Title: Re: xlat, xlatb
Post by: endrodyp on June 16, 2019, 10:08:42 PM
This last one look sympathetic, I Will try This one. Thanks a lot.
Title: Re: xlat, xlatb
Post by: aw27 on June 16, 2019, 11:51:57 PM
This version will build with MASM 5.1.2 (requires a 16-bit environment) and runs in DOS and OS/2
This is not at the level of the original question, obviously.


.model small
.stack 100h
.286

.data
wtohex_hextable   db "0123456789abcdef",0
myWord db "Word to Hex=$",0
buffer db 64 dup (0)
Sixteen db 16

.code

start:
mov ax, @data
mov ds, ax
mov es, ax

lea     dx, myWord
mov     ah,09
int     21h

mov si, offset myWord
mov di, offset buffer
mov bx, offset wtohex_hextable
mov cx, offset buffer-offset myWord
sub cx,3
next:
mov ah,0
lodsb
div Sixteen ;(result in AH:AL, not DX:AX)
xlatb
stosb
mov al, ah
xlatb
stosb
loop next

mov al, 'h'
stosb
mov al, '$'
stosb

lea     dx, buffer
mov     ah,09
int     21h

mov ah, 4ch
int 21h

end start