some c code

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Seb McClouth

Post by Seb McClouth » Tue Aug 09, 2005 8:36 am

Yes in the code on my laptop I have

Code: Select all

x=tmtime.xxx:tmtime.xxx=BCD2BIN(x)
And yes

Code: Select all

startup=mktime(&time)
is Linux source. But how can I parse tmtime to FUNCTION MkTime(value)?

I don't have mktime with me but I know a bit of the code.

But can I parse tmtime as FUNCTION MkTime(tmtime) so that in this function tmtime will be used in the calculations.

grtz
Seb

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Tue Aug 09, 2005 7:38 pm

Wyndo wrote:Something may be missing. Have you coded "mktime" into QB? The call to it still looks like C code. Also, those BCD2BIN calls will have to be changed to value=BCD2BIN(value) in order to work right. As for passing a variable by reference instead of by value, I don't remember how to do this with QB -- if it can even be done. Maybe somebody else can answer.
Variables passed to a Sub or Function are by reference.
Passing by value using BYVAL is for calling routines in other languages.

See extract below from QBasic Help.

Code: Select all

Argument Passing Conventions. 

Passing by Reference and Passing by Value  
   
BASIC uses two different ways of passing arguments to a procedure.  
   
The phrase "passing by reference," used with SUB and FUNCTION procedures,  means the address of each argument is passed to the procedure by placing the address on the stack.  
   
The phrase "passing by value," used in DEF FN functions, indicates that the value of the argument is placed on the stack, rather than the address.  
Because the procedure does not have access to the variable when an argument  is passed by value, the procedure cannot change the variable's value. 
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: some c code

Post by moneo » Tue Aug 09, 2005 8:13 pm

SebMcClouth wrote:..... I need to know what this does:

Code: Select all

((val)=((val)&15) + ((val)>>4)*10)
grtz
Seb
Why couldn't we do a simple operation like the following instead using a complicated BitRight function?:

Code: Select all

value = (value AND 15) + (INT(value/16)*10)
Dividing by 16 and throwing away the remainder is the same as shifting the value right 4 bits.

I'm confused as to why this operation is called BCD_TO_BIN. There are 3 versions of BCD (Binary Coded Decimal) that I know of:
* 4 bit BCD to represent numbers from 0 to 9.
* 6 bit BCD to represent characters including numbers, used years ago.
* 8 bit EBCDIC (Extended Binary Coded Decimal Interchange Code) used by IBM instead of ASCII.

So, what kind of BCD is this thing using, and what kind of "binary" is it supposed to be making? It would be nice to try our solutions on some real input and output data.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth » Wed Aug 10, 2005 2:50 am

What I mean is, that if I parse tmtime (not with Sec, and Min etc) to the function MkTime, can I parse it as function MkTime (tmtime)?

The function is not a C-code function.

Code: Select all

DIM Months(12) AS INTEGER
DIM mYear AS INTEGER
DIM res AS LONG

Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
Year = 365 * Day

Months(1) = 0
Months(2) = DAY*(31)
Months(3) = DAY*(31+29)
Months(4) = DAY*(31+29+31)
Months(5) = DAY*(31+29+31+30)
Months(6) = DAY*(31+29+31+30+31)
Months(7) = DAY*(31+29+31+30+31+30)
Months(8) = DAY*(31+29+31+30+31+30+31)
Monhts(9) = DAY*(31+29+31+30+31+30+31+31)
Months(10) = DAY*(31+29+31+30+31+30+31+31+30)
Monhts(11) = DAY*(31+29+31+30+31+30+31+31+30+31)
Monhts(12) = DAY*(31+29+31+30+31+30+31+31+30+31+30)


mYear= tm.Year - 70
'Magic offsets (y+1) needed to get leapyears right.
Res = Year * mYear + Day * ((mYear + 1) / 4);
Res = Res + Months(tm.Mon)
'and (y+2) here. If it wasn't a leap-year, we have to adjust
IF (tm.Mon > 1 and ((mYear + 2) \ 4)) then
  Res = Res - Day
end if
Res = Res + Day * (tm.mDay - 1)
Res = Res + Hour * tm.Hour
Res = Res + Minute * tm.Min
Res = Res + tm.Sec
MkTime = Res
End Function
I haven't tested it yet, but this is what the code should do.

grtz
Seb
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: some c code

Post by moneo » Fri Aug 12, 2005 6:36 pm

Hey Seb,

Maybe you missed my post on Aug 9th. I included it below.

Just wanted to know what you thought of my alternate way of doing the Right Shift 4 bits.

And also, could you answer my questions about the BCD and binary.

Thanks,
Moneo
*****

moneo wrote:
SebMcClouth wrote:..... I need to know what this does:

Code: Select all

((val)=((val)&15) + ((val)>>4)*10)
grtz
Seb
Why couldn't we do a simple operation like the following instead using a complicated BitRight function?:

Code: Select all

value = (value AND 15) + (INT(value/16)*10)
Dividing by 16 and throwing away the remainder is the same as shifting the value right 4 bits.

I'm confused as to why this operation is called BCD_TO_BIN. There are 3 versions of BCD (Binary Coded Decimal) that I know of:
* 4 bit BCD to represent numbers from 0 to 9.
* 6 bit BCD to represent characters including numbers, used years ago.
* 8 bit EBCDIC (Extended Binary Coded Decimal Interchange Code) used by IBM instead of ASCII.

So, what kind of BCD is this thing using, and what kind of "binary" is it supposed to be making? It would be nice to try our solutions on some real input and output data.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth » Sat Aug 13, 2005 3:20 am

Didn't miss it, I actually put in qbinux... Right and left bit left me with error all the time...

grtz
Seb
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Re: some c code

Post by Seb McClouth » Fri Mar 10, 2006 8:36 am

moneo wrote:
SebMcClouth wrote:..... I need to know what this does:

Code: Select all

((val)=((val)&15) + ((val)>>4)*10)
grtz
Seb
Why couldn't we do a simple operation like the following instead using a complicated BitRight function?:

Code: Select all

value = (value AND 15) + (INT(value/16)*10)
Dividing by 16 and throwing away the remainder is the same as shifting the value right 4 bits.

I'm confused as to why this operation is called BCD_TO_BIN. There are 3 versions of BCD (Binary Coded Decimal) that I know of:
* 4 bit BCD to represent numbers from 0 to 9.
* 6 bit BCD to represent characters including numbers, used years ago.
* 8 bit EBCDIC (Extended Binary Coded Decimal Interchange Code) used by IBM instead of ASCII.

So, what kind of BCD is this thing using, and what kind of "binary" is it supposed to be making? It would be nice to try our solutions on some real input and output data.
*****
Moneo, I finally have an answer to your question. The data which I get from CMOS/BIOS is in format that is actually (with use of your formula: value = (value AND 15) + (INT(value/16)*10))

say for example that I extracted seconds from the RTC. It will result in giving me e.g. 87... then we say... hey no can do... seconds only go to 60... thats were your formula sets it... and makes it 57... this has to be parsed back to the type so that the kernel has the right data.

grtz[/quote][/code]
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Tue Jul 04, 2006 3:32 pm

Yet another question:

I have in C:

Code: Select all

(lpc / 5000) % 100
How do I perform this in QB?

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Wed Jul 05, 2006 7:42 am

and another along with it:

Code: Select all

if (intr_count && priority != GFP_ATOMIC) 
How to put this in QB?
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

DrV
Veteran
Posts: 63
Joined: Thu Jun 02, 2005 9:44 pm

Post by DrV » Wed Jul 05, 2006 9:55 am

Code: Select all

(lpc \ 5000) mod 100

Code: Select all

if (intr_count <> 0) and (priority <> GFP_ATOMIC) then

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Thu Jul 06, 2006 1:19 pm

And other question:

Code: Select all

for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
		if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
			break;
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Thu Jul 06, 2006 7:39 pm

Seb McClouth wrote:And other question:

Code: Select all

for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
		if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
			break;
That is what I hate about C. It's so cryptic. The code doesn't "read" like Basic.

Anyway, I dug out my C manual and looked up the FOR statement.
for ( espression1 ; expression2 ; expresson 3) {
statement(s)
}

* Expression1 is used to initialize the loop counter variable.
* Expression2 is the test or condition that controls the loop. If true, then statements in the body of the loop are executed.
* Expression3 is used to modify the loop counter. Also known as the re-initialization step.

I got the above from the famous Bell Labs "C White Book" by Kernighan and Ritchie, which I bought in 1978.

So, at first glance, the code you posted is missing { and } to delimit the body of the loop. These are called squirrelly brackets, affectionately also known as Chip and Dale respectively.

Anyway, the code in QB for the FOR loop would be:

for (i = 0 ; i < 1000000 ; i++)

Code: Select all

dim i as long
for i = 0 to (1000000 - 1)
....... body .....
next i
Simple as that. Covers all the specs.
*****

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Thu Jul 06, 2006 8:24 pm

just to clarify, C doesn't need { and } if it is one line only being executed in the loop.

Same for IF statements, no need for { and } of there's only one instruction in the if statement.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Fri Jul 07, 2006 2:55 pm

Okay, got that working, thanks Moneo, it makes the c-code a lot more readable.

I have a tiny more question in C, does & and && mean the same or is there a diffrence? If so, what are there equivalents in Qbasic?

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Fri Jul 07, 2006 3:06 pm

moneo wrote:
Seb McClouth wrote:And other question:

Code: Select all

for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
		if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
			break;
That is what I hate about C. It's so cryptic. The code doesn't "read" like Basic.

Anyway, I dug out my C manual and looked up the FOR statement.
for ( espression1 ; expression2 ; expresson 3) {
statement(s)
}

* Expression1 is used to initialize the loop counter variable.
* Expression2 is the test or condition that controls the loop. If true, then statements in the body of the loop are executed.
* Expression3 is used to modify the loop counter. Also known as the re-initialization step.

I got the above from the famous Bell Labs "C White Book" by Kernighan and Ritchie, which I bought in 1978.

So, at first glance, the code you posted is missing { and } to delimit the body of the loop. These are called squirrelly brackets, affectionately also known as Chip and Dale respectively.

Anyway, the code in QB for the FOR loop would be:

for (i = 0 ; i < 1000000 ; i++)

Code: Select all

dim i as long
for i = 0 to (1000000 - 1)
....... body .....
next i
Simple as that. Covers all the specs.
*****
Why can't QB count till 999999? That's pretty annoying while in a for-next... or do I need to address i AS LONG?
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Fri Jul 07, 2006 3:14 pm

not sure if it will make a difference. try it and see..

but you could do something like.

Code: Select all

DIM MyCounter AS LONG

MyCounter = 0
DO WHILE MyCounter <= 999999
    ' whatever code
    MyCounter = MyCounter + 1
LOOP
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Fri Jul 07, 2006 7:18 pm

MystikShadows wrote:......
Why can't QB count till 999999? That's pretty annoying while in a for-next... or do I need to address i AS LONG?
The FOR can count up to the maximum that the specified variable can handle.
Well I chose to DIM the variable as LONG. I always have an DEFINT A-Z at the top of my programs, so an integer would not handle 999999.

If you don't use the DEFINT A-Z, then the variable would be SINGLE by default, which can handle 999999. I prefer the LONG because I don't need a floating point number.

In your next post you had the following sample code:

Code: Select all

DIM MyCounter AS LONG
MyCounter = 0
DO WHILE MyCounter <= 999999
    ' whatever code
    MyCounter = MyCounter + 1
LOOP
This will work fine too, but a FOR loop gives you a free increment of the "counter" variable.

MystikShadows wrote:just to clarify, C doesn't need { and } if it is one line only being executed in the loop.
Same for IF statements, no need for { and } of there's only one instruction in the if statement.
You're probably right. I haven't coded C in 10 years. The White Book has one statement examples which have the braces {}.
*****

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Fri Jul 07, 2006 8:46 pm

Seb McClouth wrote:......I have a tiny more question in C, does & and && mean the same or is there a diffrence? If so, what are there equivalents in Qbasic? ...
More examples of cryptic crap in C.

BEWARE! The & and && operators mean different things in different versions/dialects of C. I have several manuals, so I'll only give you the meaning from the C White Book.

& means "the address of". It is generally needed by some C built-in functions like scanf, and appears like: &variablename.
Another example:
px = &x;
This assigns the address of x to the variable px. px is now said to "point to" x.

& is also a bitwise AND operator.
Example:
c = n & 7;
which sets to zero all but the low-order 3 bits of n.

&& is a logical AND operator.
Can't find a decent example in the book, but it would be like substituting && for AND in the following QB statement:
if a = 0 AND b < 100 then....

Similarly,
| is a bitwise inclusive OR operator.
|| is a logical OR operator.
^ is a bitwise exclusive OR operator.

BTW, there are about another 27 of these operators using special characters.

Reading this C manual again opens up old wounds. I strongly suggest you get a copy of the C manual for the C code that you're working with.

Good luck!
*****

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Sat Jul 08, 2006 12:45 am

moneo wrote:Reading this C manual again opens up old wounds. I strongly suggest you get a copy of the C manual for the C code that you're working with.

Good luck!
*****
Moneo, your words of wisdom enlighten me (they truly do). On your suggestions of getting a copy of the C manual for the C code... I'm actually translating the real deal (Linux) to QB to have some real linux code running it, it also makes me understand C more.

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Sun Jul 09, 2006 4:05 pm

Seb McClouth wrote:
moneo wrote:Reading this C manual again opens up old wounds. I strongly suggest you get a copy of the C manual for the C code that you're working with.....
Moneo, your words of wisdom enlighten me (they truly do). On your suggestions of getting a copy of the C manual for the C code... I'm actually translating the real deal (Linux) to QB to have some real linux code running it, it also makes me understand C more.
grtz
Seb, I'm glad I could provide you with some info regarding C.

However, If you are really translating Linux code to QB, then it's imperative that you refer to the specific language that Linus was written in, otherwise it's like trying to learn Mandarin Chinese by speaking to a person from Canton.

My limited knowledge of Linux is that Linux is an operating system, not a language per se. So what is Linux written in? It could be C because Linux is derived from Unix which was written in C. If so, what specific version/dialect of C?

Then again, since Linux is open source, then it, or parts of it, could possibly be written in Java or C++. I honestly don't know. Remember that Linux has been evolving for about 20 years, and that there are several different implementations, like Ubuntu and others.

But, the bottom line is, as I said before, if you are translating code, then you need to know what language that code is written in so that you can refer to the corresponding manual.

Sorry that I don't have more definitive information. I don't envy you in your endeavors.
*****

Post Reply