flat assembler
Message board for the users of flat assembler.

Index > Main > Fasm bug demo...

Author
Thread Post new topic Reply to topic
NEOAethyr



Joined: 20 Aug 2007
Posts: 19
NEOAethyr 20 Aug 2007, 13:47
Can anyone explain why this simple code flips out?
I made this demo just to show you guys this, it's a part of my current project broke off.
I don't want to continue until I can start using the ss and ds reg's again...
And the offset regs...

The only explination I can think of for Demo_1_Bug is because it may be inputting a 32bit value into dx instead of a 16bit one.
So maybe it's inputting the result, the segment, into the offset reg...(the upper word)
That's the only thing I can think off.

I made the demo so it would be small enough to figuer out what was going on and easier to fix if it needs to be fixed.
If these are'nt bugs, then maybe I'm just stupid lol.
Maybe I'm using stuff all wrong...
I'm kinda thinking of moving to straight machine code because of these bugs though, and saying screw asm.
:/
I dn, anyways please check it out Smile.

Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; NEO's Bug Demo thingy...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

org 0x0100

jmp Init

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Notes: 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Segments:
;CS: Code Segment        ?, Can't use it for some reason.
;SS: Src Segment       Unrealiable, it'll sometimes screw over code that executes before it.
;DS: Dest Segment Unrealiable, it'll sometimes screw over code that executes after it.
;ES: Extra Segment

;Offsets:
;SI: Src Index
;DI: Dest Index
;Both are unreliable, sometimes they will point to the totally wrong offset.
;This prob only happens when they are used in combination with a segment reg, used by themselves as a single instuction they are fine.

;Old notes above are from a current project.
;I felt the need to put together this demo though after a run in with these bugs with no good way of fixing them.
;Even though Demo_1_bug is something I'm not worried about at the moment, I have to wonder how many other functions I could make and have it screw up.
;I've never had any prob with the offset regs before though, this is the only known variatin of it that I know of right now that screws up.
;So it worries me.
;Demo_2, this one worries me even more, it actually pisses me off.
;The whole idea of using ss, ds to me was for src and dest.
;And I used it like that.
;Until I found that a function of mine screwed over when I used either one for the src data.
;I tried using cs but it won't let me, the compilier won't.
;never planned on using that segment reg anyways, maybe someday the es reg, but now...
;So now I'm stuck using a single segment reg (es, the "extra" segment) because the other 2 that are usable I found to be totally unreliable.
;Needless to say, again, it pisses me off...

;About this demo:
;
;Demo_1:
;Shows the normal way to use an int to print a string
;Using ds:dx for the segment and offset, the int's own way of finding the string.
;
;Demo_1_Bug:
;Found by accident.
;I was trying to play it safe by telling dx both the segment and offset of the string.
;Instead of just the offset.
;I forgot that the int had ds setup for the segment location of the string.
;That happened because the Print_String function of mine was a page or so away from the current error reporting function code I had been working on at the time.
;Like I said, was just playing it safe, to safe and it was'nt needed at all.
;mov dx [es:si] (si is the prob)
;[es]:si does'nt work, compilier freaks.
;Does'nt make any sence though, when used correctly it should show the segment -> offset, pointing only to the offset (but then again I dn the internals of this).
;However in this case it's not, the segment is correct, but the offset is not, and it's the segment that caused the prob in the 1st place.
;
;Demo_1_Alt:
;Take the above notes for Demo_1 in hand, and remove the segment from inputing into dx.
;Now the offset is correct, even though the offset was setup the exact say way.
;It works fine, you could do the same by inputting the offset into say ax.
;The idea was t figuer out why the bug showed up in the 1st place though, and to do that I kept the code as identical as possible.
;
;Demo_2_Bug:
;Destroy's the current code and the code before it.
;Meaning, you execute code before it, which has allready ran, then run this code, it somehow goes into the past and destroy's it before it was executed.
;That's the best I can explain it, I would assume it can't go into the past lol..., but it somehow does something like that.
;Note that the src segment was used, src = kill all code before...
;
;Demo_2_Bug_Alt:
;Destroy's the current code and the code after it.
;Note that the dest segment was used, dest = kill all code after...
;
;Demo_2:
;The only way I was able to get this to work correctly the way it was meant to.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Defines:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Code_Segment          dw 0x0000

Memory_Segment         dw 0x0000
Memory_Offset              dw 0x0000

Buffer_32              dd 0x00000000

Generic_Msg                db "Hello...", 0x0D, 0x0A,  "$"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Functions:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Demo_1_Bug:
mov es, [Code_Segment]
mov si, Generic_Msg
mov dx, [es:si]
mov ds, [Code_Segment]
mov ah, 0x09
dw 0x21CD
ret

Demo_1:
mov dx, Generic_Msg
mov ds, [Code_Segment]
mov ah, 0x09
dw 0x21CD
ret

Demo_1_Alt:
mov si, Generic_Msg
mov dx, si
mov ds, [Code_Segment]
mov ah, 0x09
dw 0x21CD
ret

Demo_2:
mov es, [Memory_Segment]
mov si, [Memory_Offset]
mov eax, [es:si]
mov ds, [Code_Segment]
mov di, Buffer_32
mov [ds:di], eax
ret

Demo_2_Bug:
mov ss, [Memory_Segment]
mov si, [Memory_Offset]
mov eax, [ss:si]
mov ds, [Code_Segment]
mov di, Buffer_32
mov [ds:di], eax
ret

Demo_2_Bug_Alt:
mov ds, [Memory_Segment]
mov si, [Memory_Offset]
mov eax, [ds:si]
mov ds, [Code_Segment]
mov di, Buffer_32
mov [ds:di], eax
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Init:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Init:

mov [Code_Segment], ds

mov [Memory_Segment], 0x0000
mov [Memory_Offset], 0x0000

;Uncomment one of these at a time to see the results.
;call Demo_1
;call Demo_1_Bug
;call Demo_1_Alt

;Uncomment one of these at a time to see the results.
call Demo_1              ;Demo code execution before Demo_2 (Don't comment out) (Note that is was allready executed before the next set of code)
;call Demo_2
;call Demo_2_Bug
;call Demo_2_Bug_Alt
call Demo_1             ;Demo code execution after Demo_2 (Don't comment out)

jmp Exit

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Exit:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exit:

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
[/code]
Post 20 Aug 2007, 13:47
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 20 Aug 2007, 21:26
Your bug. Idea

Code:
Demo_1_Bug:
mov es, [Code_Segment]
mov si, Generic_Msg
mov dx, [es:si]          ; ??? Weird Confused
mov ds, [Code_Segment]
mov ah, 0x09
dw 0x21CD      ; INT $21 wouldn't do the job ? Laughing
ret
    


Also, you might want to explain what you expected, what happens instead, and why you consider it as bug. Also, your code is extremely complicated for no reason Laughing

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 20 Aug 2007, 21:26
View user's profile Send private message Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie 20 Aug 2007, 23:32
Your use of segment registers is wrong for the mode you are using. If you start your program with org 100h then the program only uses a single segment and all segment registers point to the same value by default. You also must not attempt to change this value unless you want your program to crash! In fact, you don't have to do anything with segment registers at all.
The normal way for your demo could be just:

Demo:
mov dx,Generic_Msg
mov ah,09h
int 21h
ret

Try it.

Chris

And :
Don't try to be fancy by not using int instruction.
SS stands for stack segment, not source segment.
DS is for data segment, not dest segment.
Use si for source offsets into a segment and di for dest offsets into a segment. eg:
mov al,byte[Some_location + si]
mov [Some_Other_Location+di],al
Post 20 Aug 2007, 23:32
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.