Main Menu

notint Instruction?

Started by baggey, November 16, 2012, 10:55:30 AM

Previous topic - Next topic

baggey

Why isn't this returning 8? ie 247 in 2's compement should be minus 8!?

PlayBASIC Code: [Select]
lsb=notint(247)

#print str$(lsb)

waitkey


Baggey
Jesus was only famous because of his dad

baggey

#1
So ive sort of realised. Its only for use with a string, containing 1's and 0's

Its now progressed to

PlayBASIC Code: [Select]
// Number to be inversed
// This will be between 128 and 255 to get the 2's complement negative value
lsb=247
// Invlsb will hold the inverse of lsb
invlsb=inverse(lsb)
// Display the result in console
#print str$(invlsb)

waitkey


function inverse(number)

for i=0 to 7
if not testbit(number,i)=true then inversed=inversed+2^i
next i

endfunction (inversed)



This isn't liked by the editor either?

Is this where i need to use pointers to alter the value passed. By pokeing the byte of number first passed!???
Or
Have i just tied myself up in frigging knots!?

Ie pass number to function, Inverse it. Then pass it back, so i can do something with the result!

Its going to be used to emulate the op-code JR 247.
which is go back -8 places!

kind regards baggey
Jesus was only famous because of his dad

baggey

#2
Right ok! Sussed it.  ;D

No need for brackets at the endfuction return!

Now i can generate the inverse of an interger or byte and get the two's complement of it, to see if its positive or negative.

So final routine is :-

PlayBASIC Code: [Select]
// Number to be inversed and two/s complement sign
// This will be between 0 and 255 to get the 2's complement negative value it will be > 128
lsb=128
// Invlsb will hold the inverse of lsb and sign$ will be "-" for numbers > 128
invlsb,sign$=inverse(lsb)
// Display the result in console
#print sign$+str$(invlsb)

waitkey

function inverse(number)
if number<128
signofnumber$="+"
inversed=number
else
signofnumber$="-"
for i=0 to 7
if not testbit(number,i)=true then inversed=inversed+2^i
next i
endif

endfunction inversed,signofnumber$



Baggey
Jesus was only famous because of his dad

kevin

#3
 I hope you're not going to use that..  

NotINt flips all 32bits, in the 32bit integer ..    


PlayBASIC Code: [Select]
   lsb=notint(247)
print str$(lsb)
print bin$(lsb)

sync
waitkey





  Which outputs,  


-248
%11111111111111111111111100001000



   But you only want 8bits,  so an easy approach is to mask off the top 24 bits like so

PlayBASIC Code: [Select]
   lsb=notint(247) and 255
print str$(lsb)
print bin$(lsb)

sync
waitkey




   which returns



8
%00000000000000000000000000001000



  You can get rid of the NotInt function (recommend) by subtracted the byte value from -1


PlayBASIC Code: [Select]
   lsb= (-1-247) and 255
print str$(lsb)
print bin$(lsb)

sync
waitkey





    Detecting if it's signed you either compare the value with 128 or above, or and with 128..  



baggey

#4
So we end up with,

PlayBASIC Code: [Select]
function inverse(number)

if number<128
signofnumber$="+"
inversed=number
else
signofnumber$="-"
inversed = -1-number and 255
endif

endfunction inversed,signofnumber$



which has done away with notint, for and next loop 7 times and the power addition of each bit!

Great!

Kind regards Baggey
Jesus was only famous because of his dad

kevin


why does signofnumber$ need be a string ? 

baggey

#6
Basically, when i print the opcodes in the dissambler window. The jr "-"8 will be displayed. Then im working out where its going to go ie, new PC address. But im adding the new address after the opcode as well! I think this is eaiser to understand? When reading the opcodes spat out!

Lets say were at PC 0023 it will look like :-

0023    18     F7             JR  "-" 8   ; 001C

note PC counter is at F7 ie, PC 0024-08=1C

"-" being the sign string bit!

I Haven't tried it, But. If i have -8 stored in lsb. The #print LSB would be "-8"?

Just to see?

PlayBASIC Code: [Select]
lsb=-8
#print lsb
waitkey



Genius! So we could trim some more out  :D

PlayBASIC Code: [Select]
function inverse(number)

if number<128
inversed=number
else
inversed = -1-number and 255
endif

endfunction inversed



So my GOSUB! for jr n looks like this at the MO. This is a uniform structure im using for all codes!

PlayBASIC Code: [Select]
op_24:  // JR n

nxtpc=2

address=pcptr+pc
lsb=peekbyte(address+1)
num=msb*256+lsb

if execute=false
invlsb=inverse(lsb)
if sign$="+"
jump=pc+nxtpc+lsb
else
jump=pc+1-invlsb
endif
if hex_or_dec = false
// Decimal representation
op$=str$(op_byte)+" JR n"
else
// Hex representation
op$=right$(hex$(pc),4)+" "+right$(hex$(op_byte),2)+" "+right$(hex$(lsb),2)+" JR "+sign$+str$(invlsb)+" ; "+right$(hex$(jump),4)
endif

; no flags affected
fs = fs
fz = fz
f5 = f5
fh = fh
f3 = f3
fpv = fpv
fn = fn
fc = fc

endif
exeTstates = 12
return ;done



Ive now altered a section of this code! But im not getting the MINUS!?  ???

PlayBASIC Code: [Select]
if execute=false 
invlsb=inverse(lsb)
if invlsb<>-8
jump=pc+nxtpc+lsb
else
jump=pc+1-invlsb
endif
if hex_or_dec = false
// Decimal representation
op$=str$(op_byte)+" JR n"
else
// Hex representation
op$=right$(hex$(pc),4)+" "+right$(hex$(op_byte),2)+" "+right$(hex$(lsb),2)+" JR "+str$(invlsb)+" ; "+right$(hex$(jump),4)
endif



Baggey
Jesus was only famous because of his dad

baggey

Ive now reverted back to using the signofnumber$ being either "+" or "-" and building the string of opcode, that way!

Its working  :)

Baggey
Jesus was only famous because of his dad

kevin


  normally a diss-assembly will give the absolute address of the branch, so you don't need to display the direction.

baggey

#9
Hmm?

Heres how the dissembler is coming on! But ive been using the sign and the address it goes to. Its actually the Spectrum ROM, machine cod program held in memory.

Which is the absolute address?

Oh! Im making some graphical changes as well.

Sorry about that one! It didn't look that good until it was shrunk.

Baggey
Jesus was only famous because of his dad

kevin


Please choose a more appropriate picture.. 

baggey

Im going to be running into some Z80 opcodes soon. Where im not sure what the proper effects are?

I shall use the thread Z80 op-codes for this in Chat? Not sure were to discus these really!?

Kind regards Baggey
Jesus was only famous because of his dad