Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-20-2004, 03:03 PM   #1 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
Red face Writing a binary value

I am practicing writing some simple but useful procedures for my x86 assembly stuff. I have tested this and it works fine, but whenever I use any sort of and, neg, or, xor instruction it does strange things to the value that I do it to.
I can't figure out if it is my procedure to write the value to the console that is screwy, or if I am just making a dumb mistake.
Code:
WriteBin proc
	; save registers
	push cx
	push dx
	push si

	mov cx,16	; number of bits to display
	
	mov si,OFFSET bBuffer
	_WRITE:
		shl ax,1	; shift bit into carry field
		mov BYTE PTR [si],'0'
		jnc _ZERO ; is it a zero?
		mov BYTE PTR [si],'1' ; no, change it to 1
	_ZERO:
		inc si		; increment cx
	loop _WRITE
	; Display the binary value in bBuffer
	mov dx,OFFSET bBuffer
	mov cx,LENGTHOF bBuffer
	call WriteString ; another function I made that outputs a string pointed to by dx
	; restore registers
	pop si
	pop dx
	pop cx
	ret
WriteBin endp
The following code produces this output:
0000000000000001
0000000000000000
Code:
; other stuff (taken out to shorten)
main proc
	mov ax,@data	; set up segment
	mov ds,ax
	
	mov ax,1
	call WriteBin
	endl ; A macro I made that outputs a new line
	and ax,1
	call WriteBin
	.exit
main endp

; other code (taken out to shorten)
Maybe I am overlooking something stupid but shouldn't "and 1,1 = 1"?
So can anyone see a problem with my WriteBin procedure or some other stupid mistake? This has really started to agitate me.
(I am using masm as my assembler)

Thanks

Last edited by Amaranthine; 06-20-2004 at 03:46 PM.
Amaranthine is offline   Reply With Quote
Old 06-20-2004, 06:09 PM   #2 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
AHA!!!
well, I figured I might as well let you know I found my mistake, and indeed it was a stupid one.
If you look at my WriteBin procedure you will notice I forgot to push or save the value of ax in any way. Since it get's shifted left 16 times it would always be 0 by the end of this procedure!
Anyway I just added a push ax and pop ax to it and now I don't always come up with zero.

But I would like to take this moment to ask another question, I have found a few very good uses for the xlat instruction but I never seem to be able to make them work. An example of one I am trying to make work(but it doesn't) is this:
Code:
; digitTable = "0123456789ABCDEF"
Al2Ascii proc
        push bx        ; save regs
	push ds
	sub bx,bx      ; set ds to zero
	mov ds,bx
	mov bx,OFFSET digitTable	; get digit from table
	xlat
	pop ds
	pop bx         ; restore regs
	ret
Al2Ascii endp
But it doesn't work (is my logic flawed?)
If anyone could give me a working example (or spot my error) of xlat using ds:bx (not ebx) I would greatly appreciate it.
Thanks yet again.
Amaranthine is offline   Reply With Quote
Old 06-20-2004, 07:37 PM   #3 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
wont sub bx, bx put the result in eax ?

it's been a long time since ive done any assembly ...
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-20-2004, 07:41 PM   #4 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
info about xlat:
Quote:
6.4.3 The XLAT Instruction
The xlat instruction translates the value in the al register based on a lookup table in
memory. It does the following:
temp := al+bx
al := ds:[temp]
that is, bx points at a table in the current data segment. Xlat replaces the value in al with the
byte at the offset originally in al. If al contains four, xlat replaces the value in al with the
fifth item (offset four) within the table pointed at by ds:bx. The xlat instruction takes the
form:
xlat
Typically it has no operand. You can specify one but the assembler virtually ignores it.
The only purpose for specifying an operand is so you can provide a segment override pre-
fix:
xlat es:Table
This tells the assembler to emit an es: segment prefix byte before the instruction. You must
still load bx with the address of Table; the form above does not provide the address of
Table to the instruction. Only the segment override prefix in the operand is significant.
The xlat instruction does not affect the 80x86’s flags register.
you don't seem to put anything in al, so there's your prob
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-20-2004, 08:21 PM   #5 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
It's a procedure so I give al a value and then call the procedure
Code:
.data
digitTable db "0123456789ABCDEF"

.code
main proc
    mov ax,@data
    mov ds,ax

    sub ax,ax
    add al,7
    call Al2Ascii
    .exit
main endp
end main
or were you meaning something else?
Amaranthine is offline   Reply With Quote
Old 06-20-2004, 08:44 PM   #6 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
nope, that is what i meant.

what result do you get ?

also, your code is a bit confusing, why do you
use sub to set to 0 ? from what i've seen, expected
notation is:
Code:
xor reg, reg ; e.g: xor bx, bx
and its also a bit confusing to see you set a reg
to 0, then move a value into it (basically the
setting-reg-to-zero is useless).
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-20-2004, 09:26 PM   #7 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
sub reg,reg
is much faster than
mov reg,0
that is why I use that to set something to zero, also, I want ds to be zero and you can't do this:
mov ds,0 or this sub ds,ds
so I need to set bx to zero then move it to ds to make ds zero. (sorry if I misunderstood what you were saying, I'm not quite sure if that was what you meant)

Here is the output:
twelve dw "12" ; another var

mov ax,12 ; decimal value
ax 12: 0000000000001100

call Ascii ; procedure is above (grabs from digitTable)
Ascii ax: 0000000000001110

mov ah,'1' ; ax="12"
mov al,'2'
12 ax: 0011000100110010

mov ax,twelve ; the value of twelve, twelve="12" (a word made above)
twelve ax: 0011000100110010
Amaranthine is offline   Reply With Quote
Old 06-20-2004, 10:00 PM   #8 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
Quote:
I want ds to be zero and you can't do this:
mov ds,0 or this sub ds,ds
so I need to set bx to zero then move it to ds to make ds zero. (sorry if I misunderstood what you were saying, I'm not quite sure if that was what you meant)
i see, i didn't realise that

Code:
sub reg,reg
is much faster than
mov reg,0
yes, i wasn't suggesting mov reg, 0 I was suggesting xor reg, reg which is faster then sub reg, reg

im a bit confused about your input in quotes, to my mind
this would be the ascii value of 1, not "One" is that what
you want ?

i.e. your table doesn't have an element at the ascii value
of '1'

but you're right, your code doesn't seem to work if
it gets back '1110' for an input of '12'.

hmm.

---
however, i just looked back quickly, and why are you
setting ds to 0 ?
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-20-2004, 10:26 PM   #9 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
xlat returns a byte in a string pointed to by ds:bx.
So I don't want ds to have any value that would screw it up.

and to your question, yeah, I wanted the ascii value of one, I am making a procedure that outputs a value in ax as hex. (I have one I made above that does it in binary) (though now that I think about it I wouldn't want twelve to be "12" I would want it to be "C", however, it still doesn't work)

any ideas as to why it isn't working?

Since ax has 12, I would think my Al2Ascii proc would set al to 'C'.
Since bx is pointing to digitTable and C is the 12th byte.
Am I simply thinking wrong?
Amaranthine is offline   Reply With Quote
Old 06-20-2004, 10:34 PM   #10 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
well reading from the text i quoted you can't
convert ax as a whole using xlat, only al, you
would have to shift ax >> 8 to translate both.

Quote:
Since ax has 12, I would think my Al2Ascii proc would set al to 'C'.
yes, i would think that too ...

try not cancelling out ds (ds == data segment ?) and see if
it works.
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-20-2004, 11:53 PM   #11 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
yeah, ds is the data segment.
cs I think is the code segment though I am not positive.
But what is es, I never learned about it?

oh duh!!
I'm such an idiot, thankyou for pointing that out.
I don't know why I was thinking must set ds to 0.
It works now.
(I wonder what random value I was jamming into bx?)
Amaranthine is offline   Reply With Quote
Old 06-21-2004, 03:15 AM   #12 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
i've never heard of es either, maybe you mean 'esi' ?

you would've been putting the 12'th byte of your entire program into the proc, and you could probably find that with a hex-editor on the exe
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 06-21-2004, 11:29 AM   #13 (permalink)
Amaranthine
Registered User
 
Amaranthine's Avatar
 
Join Date: May 2004
Posts: 47
Amaranthine is on a distinguished road
ah, I think I may have found it (though it's not very descriptive), es is the extra segment register
Amaranthine is offline   Reply With Quote
Old 02-20-2006, 11:28 AM   #14 (permalink)
Hamlin
Jedi Master
 
Hamlin's Avatar
 
Join Date: Feb 2006
Location: Denver, CO
Posts: 15
Hamlin is on a distinguished road
Looks like this one is done, too. If there's no further questions, we'll let it drop off the roster.
Hamlin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
php script that can upload binary data infinite_root PHP 9 08-27-2004 06:03 PM
Loading Binary Data infinite_root PHP 2 08-23-2004 10:14 PM
writing basic functions? Admin PHP 4 11-25-2003 12:43 PM
Binary I/O file reading (0x1a trouble) Danish Standard C, C++ 2 05-26-2003 10:02 AM


All times are GMT -8. The time now is 02:10 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting