flat assembler
Message board for the users of flat assembler.

Index > Windows > Please Help me Understand

Author
Thread Post new topic Reply to topic
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 21 Nov 2010, 22:46
First off I'm new to IA-32, not assembly in general.

But I have a comprehension issue here. I have written an I8080 emulator in a language that uses FASM(PureBasic). My problem is I don't understand why 32bit compare functions correctly with a byte, but then an 8 bit compare with the same byte sets the flags wrong.

Here is 32 bit compare snippet that tests okay with my CMPA Core Test Function, which test zero, sign, and carry flags. And the emulator functions fine with this, cause it just running some not so advanced 8080 code.
Code:
   ;===============================================================
   ;8080 Stuff
   ;===============================================================
   mov eax, [v_InValue]        ;move invalue to eax
   cmp  [v_RegisterA] , eax    ;compare A and invalue
   setZ byte [v_FlagZero]      ;set zero flag, Zero means Equal
   setL byte [v_FlagCarry]     ;Set if Greater, reset otherwise
   SetS byte [v_FlagSign]      ;set Sign Flag
   pushfd                      ;push flags so I can test for halfcarry
   pop dword eax               ;pop into eax
   mov [v_ReturnValue],eax     ;so I can look at flags
   bt eax,8                    ;test for halfcarry
   setC byte [v_FlagHalfCarry] ;set halfcarry flag
   ;===============================================================
   ;==================================================================
   ;z80 stuff
   ;==================================================================
   mov dword [v_FlagN],1; set for Z-80
   SetO byte [v_FlagParity]; This is for overflow in Z-80,
   ;8080 manual mentions this flag is affected, but not how.
   ;So...set-z80 style
   ;==================================================================    

But since this is an 32bit compare I know, deep down, that overflow and halfcarry(AF) cannot be correct. Though I have written code to check them yet.

Problem is, when I try to do a real 8 bit compare with AL, flags are never set right, and of course it fails core test and emulator malfucntions.



Code:
   ;===============================================================
   ;8080 Stuff
   ;===============================================================
   mov al, byte [v_InValue]        ;move invalue to eax
   cmp  byte [v_RegisterA] , al    ;compare A and invalue
   setZ byte [v_FlagZero]      ;set zero flag, Zero means Equal
   setL byte [v_FlagCarry]     ;Set if Greater, reset otherwise
   setS byte [v_FlagSign]      ;set Sign Flag
   pushfd                      ;push flags so I can test for halfcarry
   pop dword eax               ;pop into eax
   mov [v_ReturnValue],eax     ;so I can look at flags
   bt eax,8                    ;test for halfcarry
   setC byte [v_FlagHalfCarry] ;set halfcarry flag
   ;===============================================================
   ;==================================================================
   ;z80 stuff
   ;==================================================================
   mov dword [v_FlagN],1; set for Z-80
   SetO byte [v_FlagParity]; This is for overflow in Z-80,
   ;8080 manual mentions this flag is affected, but not how.
   ;So...set-z80 style
   ;==================================================================    


Any help to understand what's going on with FASM and or just IA-32 in general would be greatly appreciated. Thank you.
Post 21 Nov 2010, 22:46
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4182
Location: vpcmpistri
bitRAKE 22 Nov 2010, 15:58
On IA-32 the halfcarry is the AF (aux flag, bit 4), see Vol.1 Sec 3-4-3 of the manual. IMO, easiest will be to grab the flags directly (SAHF or PUSHF/POP reg32). AF and PF are always based on low byte of operation - regardless of size of operation. AF is hard to get at because IA-32 doesn't expose it to SETcc or Jcc, and has been virtually deprecated in modern 64-bit environments.
Post 22 Nov 2010, 15:58
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 22 Nov 2010, 16:47
Thank you for your answer and pointing out my BT location error.

I think my error 8086+ does a signed compare?

The 8080 manual states: Note: CMP treats (A) And (REGM) As unsigned

I cannot find anywhere in a manual from 8086+ to IA-32 a note on how the compare is carried out besides sign extension if source and destination are different sizes.

So if I deduce correctly, 8086+ does a signed compare on 8 bit, that I'm going to have to stick with native long long compare, to avoid signs on my 8 bit value, and spend some cycles to determine if an overflow happened on the 8 bit value. This is not hard to do on the z-80. Basically if the sign changes then there is an overflow.

I just wanted the IA-32 microcode to do this work to save some cycles on this 8080 core function.

Thank you for your time.
Post 22 Nov 2010, 16:47
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4182
Location: vpcmpistri
bitRAKE 23 Nov 2010, 02:42
It does both signed and unsigned. Very Happy

CF/ZF is the result of unsigned.
OF/SF/ZF is the result of signed.

I agree with your original assessment that it's just an issue of comprehension - need translate some of the terminology to understand where instruction sets meet. Unfortunately, I started with the 80286, so my understanding of the 8080 is limited.

Yet to find an 8080 instruction manual, but other info on the web indicates all the CP* instructions operate on register A and another -- all are 8-bit. A byte comparison in IA-32 should work. The architectures seem very similar.
Post 23 Nov 2010, 02:42
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4182
Location: vpcmpistri
bitRAKE 23 Nov 2010, 03:35
Okay, I was able to find an instruction manual:
http://www.tramm.li/i8080/Intel%208080-8085%20Assembly%20Language%20Programming%201977%20Intel.pdf

(Unrelated: Also stumbled upon stash of older Intel documents:
http://www.bitsavers.org/pdf/intel/ Sometime easier for people to learn x86 from the 8086 docs - current manuals are so huge.) Cool

So, the instruction is the same mnemonic CMP. The carry flag is even the same. You'll want to use "setC byte [v_FlagCarry]", or better yet: store all the flags because 8080 CMP effects ZSPCA flags (just like x86).

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 23 Nov 2010, 03:35
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 24 Nov 2010, 22:24
Thank you very much. you helped alot, seems carry works the same, but sign flag is set different on 8 bit compare than 8086 as it fails tests. I believe that with some type of bit compare with Overflow and Sign, or, and, not, etc... May give me the same sign set as the 8080. I'm sure they didnt do a complete microcode overhaul from 8080-8086. I'm sure the 8080 has an overflow internally and its just not exposed and they use this to somehow help calculate the sign flag. On the 8086 it is exposed, and this is the reasoning of my theory.
Post 24 Nov 2010, 22:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4182
Location: vpcmpistri
bitRAKE 25 Nov 2010, 03:16
Page 1-10, of the manual referenced above states the SF is just the high-bit of the result (bit 7, when referencing byte operations). This is the same as x86. What difference are you experiencing?
Post 25 Nov 2010, 03:16
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 03 Dec 2010, 15:32
Sorry it took so long for reply, but I got back to the issue just now.

You are right, I had a small bug in my 8080 crc32 Calculation. My micro kernel was stealing a register during a print which caused the crc32 to fail.
8080 assembly is kinda confusing. Confused


Thanks for your help. Very Happy It is greatly apprecitated.
Post 03 Dec 2010, 15:32
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 06 Dec 2010, 06:59
ajtaji
can we see your emulator and does it have any debugging tool?
Post 06 Dec 2010, 06:59
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 07 Dec 2010, 05:56
Well, the emulator itself is big pile of junk right now. But ya, you can look at it.

Right now I'm in the middle of optimizing video and trying to figure out how to get interupts to fire like they did on the Original Space invaders machine.
Download here:
My Roms are not to be distributed outside this package, unless you know someone that may want to run them on their space invaders machine and post the results. Thanks.
https://sites.google.com/site/ajtaji/stuff/8080CoreExperiment.rar?attredirects=0&d=1

Included is my core crack roms(meant to be run on the original Space invaders machine). Might wanna lower the fps and pump up the Mhz for these crc32 calcs. They are a little much for a 8 bit register updater that the 8080 was. I cant supply any other roms, but, if you find a 4 rom set for space invaders ,in your garage, you can rename the files and place them in the rom directory and play space invaders.

order or load h,g,f,e. 0k,2k,4k,6k offset

I dont know what you mean by debugging tools. I have a runtime disassembler built in. And I have a few helpers built in to point me in the right direction when the emulator belches. The disassembler is not compiled into this version. Very Happy I don't have a micosoft suite debugging system for this experiment. It's written in PureBasic and hand assembly.
Post 07 Dec 2010, 05:56
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 07 Dec 2010, 06:15
thanks! i'm interesting in emulator only, not in roms. i still have some spare 8080 and 8085 cpus and like its assembler from the old days. btw, you may use even fasm to code for 8080/85, the only issue with that realisation - it has not full syntaxis checking implemented.

regards!
Post 07 Dec 2010, 06:15
View user's profile Send private message Visit poster's website Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 07 Dec 2010, 06:34
well, i made a look on it and i see it is game-specific. unfortunately, i have not even heard about Space inviders machine before Smile if you share memory map, screen and keyboard organisation of that machine it could be possible to use another programs to run on emulator. just for interest Smile
btw, i'm using that emulator: http://bashkiria-2m.narod.ru/download.html - emu.rar
it's quite flexible, but i'm not sure if i could emulate that machine on it by myself. if there were info i told above, you could contact the author of emu to know if it is easy to implement. that emulator has built-in 8080 debugger - you may stop running program whenever you want and provide step-by-step execution Wink

regards!
Post 07 Dec 2010, 06:34
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 07 Dec 2010, 13:06
Yes, you are right, it is machine specific. This my first emulator, so I picked the simplest machine I could find to build on top of my core. The space invaders Arcade machine.

Looking at that emulator you pointed out(although I don't speak Russian), it is a z-80 emulator. The z-80 is only semi compatible with I8080. Extreme core logic difference in a minute sense.

Everything you want to know about the Space invaders machine can be found here:
http://computerarcheology.com/spaceinvaders/spaceinvaders.txt.html.
The only difference Is I have 64kram, some of which will be used when you load roms and the bugs I may have.
Post 07 Dec 2010, 13:06
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 07 Dec 2010, 13:40
i see. emu is many cpu emulator, including z80, 8080, 6502 and pdp. you could be puzzled with russian, as there are names of soviet clones used: 580BM80 for 8080 and 1801BM1/2/3 for PDP. personaly i made a 8080-based pc you could find by the first my link. you can also find there the demo with sources and video how does it look when run Wink

what about sp.invaders - it is interesting, i see it is fully disassembled and commented, just not handy as it is disassembled in z80 mnemonics, what puzzles me as i'm accustomed mostly to pure 8080 Smile z80 mnemonics "automatically" bring z80 architecture to the head, so, it is hard to think about 8080 then Smile

regards!
Post 07 Dec 2010, 13:40
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 07 Dec 2010, 16:21
I find z-80 easier to understand when looking for problems. 8080 assembly doesn't seem logical to me for quick looks.

Here is the code in 8080:

http://computerarcheology.com/spaceinvaders/spaceinvaders8080.txt.html

I saw your board, that is nice. If you have a true Intel 8080(not a clone) laying around, I'd share my 8080 core crack source, if you wouldn't mind doing a few adjustments for print routines and interrupts. Its about a 4-8hr run on a true 8080 by my calculations Sad. Can run it in my emulator in under 1 min on my desktop though.

I hope you are interested in a 4-8 hr burn in of your 8080 computer. It looks nice Smile
Post 07 Dec 2010, 16:21
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 08 Dec 2010, 06:10
thanks for 8080 mnemonics. in real it's also logical, just different logic Smile it is better for me as i've been coding a lot for 8080 and about nothing for z80. although i have no original i8080, the soviet 580BM80 clone was made as a copy of i8080 and thus had same bugs as i8080, 580BM80A is clone of corrected i8080A. i have couple of 580BM80 also, but they are in flat 48-pins package and i'm not sure if i can attach them to 40-pin DIP socket. sure, i can run any test you want on real 8080A board, just later, maybe on new year holydays, as i need some time to assemble it back (it is now disassembled and stored in different boxes Smile )
--------------------------
also i have to say, the board which i have does not use hardware interrupts and separate interface for io (io included into memory map) - this may reduce capability for testing. to add these features it will need dramatical update. it could be quiet easy if there 8228/8238 were used, but it was a cheap implementation and does not have it. 8080 does not have outputs like INTA or IO/MEM, to get them without 8228 an additional register will be required to get machine status byte by F2 strobe.
regards!
Post 08 Dec 2010, 06:10
View user's profile Send private message Visit poster's website Reply with quote
ajtaji



Joined: 21 Nov 2010
Posts: 8
ajtaji 08 Dec 2010, 22:38
I see. Shocked You have sparked my interest on the specialist machine.

I think I may begin converting my hardware map over to specialist computer in the near future. It is another extremely simple machine. Only I would have to add a custom clock chip to it. Without interrupts, there would be no way to time the 8080 code that ran at 250mhz. A small demo would end in an instant without interrupts to use as timers. There is not at least a V blank interrupt? How do you time your output to the screen? There had to be some kinda connection between cpu/ram & video, as ram needs refreshed(unless it was static), also video access and cpu cant clash. And these clocks had to be sync'ed. Perhaps this machine is simpler than I can imagine. But this machine sounds like what I want to emulate to work on my 8080 core, and add my sound chip and custom graphics experiments to.

Thanks for the info, btw, interrupts in my software only run the clock. Code only test core instruction results and flag results. And they are isolated with what looks like poor coding. I have several push/pops between flag changing instructions to keep them preserved for the final crc.


But I believe I'm off to study the memory map and video of the specialist and convert my memory map and font to match the screen orientation of a normal machine like the specialist. Cheers.
Post 08 Dec 2010, 22:38
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 09 Dec 2010, 05:57
the specialist has no any interrupts used. it has fixed 2MHz clock, and time delays usually are estimated by time of execution of certain commands. as there is no any external influence on program execution, that estimation is usually enough precise, conditional jumps/calls/rets are usually hard to predict, but they give small deviation. i have no study or develop any game for it (only been searching the way to get immortality etc. Razz ), but i think they are executing everything in loop, and convenient speed is reaching by delay in loop, which can be adjusted.
the specialist has dinamic ram, but it is refreshing by video generator transparently for cpu - video generator accesses memory between cpu memory access cycles.

regards!
Post 09 Dec 2010, 05:57
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.