General Discussions

Input with c without using libraries – gingerellies

gingerellies

Member

Posts: 50
From: Orlando, FL
Registered: 05-28-2002
Does anybody out there know how to get input and output in C without using the standard libraries? I am at a complete loss. I have looked all over the net and can find nothing. Please help if you can. Or let me know if it is even possible. Thank you! God Bless!

------------------
God bless and keep you!
-Ginger

graceworks
Member

Posts: 455
From: Corvallis, Oregon, USA
Registered: 03-03-2001
Hi Ginger!!

What do you want input from and what will you output to?

If you answer "keyboard" or "file" or similar to those questions, it follows that you would have to define "how to" access those. Which 'may' already be done for you in a library.

Hopefully some of the smart people can see beyond my thinking and really answer your question!

Look to Jesus,
Tim

------------------
Called by God. The passioned plea of a father. The journey awaits at Jarod's Journey.

gingerellies

Member

Posts: 50
From: Orlando, FL
Registered: 05-28-2002
Hey! Good to hear from you! I'm just working on getting this test done. I am stumped on this one c program I am supposed to write, I have it all done excaept they want me to not use any c libraries and I did all the other stuff without using the libraries, but I never learned how to do input/output in regular c without using libraries. I have looked through all my books and all over the net and there is just nothing.
It is just the basic from the keyboard to the screen. I know it would be so easy to do it with the libraries, but I need to do it without using any c libraries. Is that even possible? Am I going to have to use assembly?

------------------
God bless and keep you!
-Ginger

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Are you sure you're reading the question right? Standard C libraries exist because, among other reasons, so you don't have to get so low-level that you're basically working at the hardware level. In other words, it's possible to write code that doesn't use the standard C libraries, but then you're writing code for specific hardware - i.e. what works for an x86 Intel-based system is not going to work on a Mac or a Solaris, etc.

Also, yes, you're going to have to resort to assembly, at least inline assembly. Which technically means you're not doing it in C anyways, which again makes me wonder if the question is worded correctly. So not only does the hardware have to be defined before hand, but so does the compiler, because each compiler handles inline assembly differently.

So, assuming the question really means no standard C libraries, and you're on an x86 Intel system with a recent Microsoft compiler, I would use the various functions of DOS interrupt 21h and inline asm. Something like this (this is uncompiled code, and I haven't done this sort of thing in several years, so it probably won't even compile but hopefully it will at least point you in the right diretion):


int main()
{
char mychar; // char to receive keypress
char message[100] = "Your key was: $";
char keymsg[10] = "x$";

__asm
{
mov ah, 1 ; function 01h = read keyboard
int 21h ; call DOS interrupt 21h. Calling with
; function 01h will return the key character in AL.
mov mychar, al ; save the contents of AL in our variable
}

// now output it to the screen
__asm
{
mov ah, 8 ; function 08h = read keyboard without echo
int 21h ; call DOS interrupt 21h. Calling with
; function 08h will return the key character in AL.
mov mychar, al ; save the contents of AL in our variable
}

// save the keypress in the output string
keymsg[0] = mychar;

__asm
{
; display the first part of the message
mov dx, seg message ; make DS : DX point to message
mov ds, dx
mov dx, offset message
mov ah,9 ; function 09h = write string ($ terminated)
int 21h ; call DOS interrupt 21h

; display the second part of the message
mov dx, seg keymsg ; make DS : DX point to keymsg
mov ds, dx
mov dx, offset keymsg
mov ah,9 ; function 09h = write string ($ terminated)
int 21h ; call DOS interrupt 21h
}

return 0;
}


------------------

[This message has been edited by BrianT (edited February 22, 2004).]

gingerellies

Member

Posts: 50
From: Orlando, FL
Registered: 05-28-2002
Wow, That sure is a lot to wake up to. Thank you tons and bunches! That helps a lot. At least now I know to research assembly to get what I need and make sure that is what I need. I need to understand what I write. Anything less would be dishonest. And, hey, I wanted to learn some assembly anyway. No time like the present . Oh, here is exactly how the question was worded.

Write an optimized C program to convert a base 16 number to its base 10 equivalent, without using the standard C libraries, i.e. don’t use scanf(“%x”). The program should allow the user to input the number. State any assumptions you made when writing the program. Use 23DA as a test number.

I guess the assumptions they were talking about was the processor and compiler used, like you mentioned. Again, thank you! That was exactly what I needed to get unstuck. God bless you!

------------------
God bless and keep you!
-Ginger

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Hey,

Ah, I think assembly may not be needed. The way the question is worded, I think it means you can't use libraries only for the conversion aspect - i.e. you can't use scanf("%x", &val), because %x is for hex values and would make conversion trivial (you'd just "printf("%d", val) and you're done). I'm wondering if the question means something like you can use scanf("%s", buffer) or gets(buffer) to read in input as a string (and then you assume it is a hex number, and then convert that to decimal using your conversion code (without using standard libraries) and then spit out the result with a simple printf("%d", val) . You should maybe ask your instructor for clarification if he means no standard libraries for the whole program, or just for the conversion.

If it's just for the conversion, you won't need assembly at all, and I can help you with that too (although I'll try not to do you're homework for you. )

------------------

gingerellies

Member

Posts: 50
From: Orlando, FL
Registered: 05-28-2002
Well, I have the whole program done. Then I realized that, first of all, I used c++ and I will have to change a few key things. I am using cin and cout, but that was the ONLY things I used for libraries. But, I don't think I can use those with regular c, so I will need to change that anyway. Not using assembly sure does make me smile, I would love that. I think I am just going to use the basic input/output functions and be done with it and put that in my assumptions. It's not like I used the function for any part of the conversion process. That should be ok. I hope. Thank you so much for your help!

------------------
God bless and keep you!
-Ginger

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
OK You got the conversion working without libraries? Good job!

------------------