473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Explicit cast to convert

Hi All,
I am writing a program at the moment that will allow me to manually read sectors on a floppy disk and have come into a bit of bother. The program is designed to run in a DOS32/16 environment so I am using the digital mars c compiler compiling c and some inline assembly language with the parameters 'dmc filename -msd', but i am having issues with the compiler giving me the error: explicit cast to convert parameter 1 from int* to int when trying to give a memory location to a pointer. I believe the problem may lie with the compiler restricting these operations because they may be considered dangerous but they are needed to directly reference the memory. Can anybody offer me any advice?

The section of code giving me bother is:

int *memsectptr
memsectptr = (int *)0x1000;
for(i=0; i<(sectorsize); i++)
{
fputc(*(memsectptr+i), outfileprev); /*get memory values before use*/
}

Thanks,
Dinklebaga
Feb 13 '09 #1
20 3306
donbock
2,426 Expert 2GB
What is the type of variable "i"?
Specifically which line is indicated in your error message?
Are you getting a compiler error or a compiler warning?

*(memsectptr+i) uses pointer arithmetic style. This looks ok assuming "i" is an int.

It doesn't solve your problem, but depending on the rest of your program you might prefer this declaration:
Expand|Select|Wrap|Line Numbers
  1. int * const memsectptr = (int*) 0x1000;
The difference here is that the value of the pointer is initialized to 0x1000 and the compiler will not allow you to change it.
Feb 13 '09 #2
Hi Donbock,
Firstly thankyou for replying. Yes i is set as an integer just simply to hold the value of the sectorsize (512, 2048, etc.) but what i am getting is compiler errors which stop me from compiling the program. The section of code that i have posted is actually only a small section of the entire program but it is this section which is causing me grief. Like you have observed I do want to have the value of the pointer set to 0x1000 but I then want to access the data that is stored at the pointer (the 512 bytes that start at the pointer). Usually i would use malloc but because the program is partly in asm I need to reference the memory location directly.

Thanks,
Dinklebaga
Feb 13 '09 #3
donbock
2,426 Expert 2GB
Does the error message refer to a specific line number? If so, which line is it?

I assume that the error is either for the line where you assign 0x1000 to memsectptr or to the line where you call fputc.

If the error is for the call to fputc you might try declaring a new int* variable, assigning memsectpr+i to that new variable, and then passing that variable to fputc.
Feb 14 '09 #4
The error is occurring on the fputc line. I have created a new pointer (int * tempptr) and then assigning the value of that to the fputc statement like so:

fputc(tempptr, outfile);

but it still kicks out an error and won't compile. The only time it will let me compile is when it is written like this:

fputc(*tempptr, outfile);
Feb 14 '09 #5
JosAH
11,448 Expert 8TB
@dinklebaga
No surprise: the fputc() function takes an int as its first parameter, not a pointer.

kind regards,

Jos
Feb 14 '09 #6
Hi Jos,
Thanks for your quick response but unfortunately this still isn't solving my issue. I input a new int tempval and replaced tempptr with this but once again i am getting compiler errors being kicked out but on the line previous to the fputc statement:

for(i=0; i<(sectorsize * sectorct); i++)
{
tempval = memsectptr+i;
fputc(tempval, outfile);
}
complaining of the explicit cast to convert int * to int

Thanks,
Dinklebaga
Feb 14 '09 #7
JosAH
11,448 Expert 8TB
@dinklebaga
You're doing it again: memsectptr+i is a pointer value; fputc() takes an int as its first parameter. Maybe you want memsectptr[ i ]

kind regards,

Jos
Feb 14 '09 #8
Thanks Jos, that seems to have cured one of the problems with the code but I am still not receiving the correct output from the program. In the ASM code I am explicitly reading the sector to 0x1000 from the floppy disk but i am not getting different results from when i run the fputc function before and after the asm is run (into different files to compare) which leads me to assume that the C code is not referencing the memory correctly
Feb 14 '09 #9
JosAH
11,448 Expert 8TB
C doesn't make 'mistakes' like that; more likely nothing is read from the floppy disk and no memory contents is changed.

kind regards,

Jos
Feb 14 '09 #10
I'm not sure where the code is wrong then because it is the boot sector that is being read by the asm code and i have downloaded a hex editor to view what code is actually in the sector and sure enough it is there?
Would it be handy to post the program as a whole?
Dinklebaga
Feb 14 '09 #11
JosAH
11,448 Expert 8TB
@dinklebaga
Sure, feel free to post the relevant parts because it's no use to keep on guessing.

kind regards,

Jos
Feb 14 '09 #12
int status;
char * const memsectptr = (char *) 0x1000;
int *tempptr;
int sectorsize = 512;
int sectorct = 1;
int i;
FILE *outfile;
FILE *outfileprev;
outfileprev = fopen("hexdumpprev.txt", "wb");
outfile = fopen("hexdump.txt", "wb");
memset(memsectptr, '\0', sectorsize);

for(i=0; i<(sectorsize); i++)
{
fputc(memsectptr[i], outfileprev); /*get memory values before use*/
}

asm
{
mov bx,0x1000;
mov es,bx;
mov bx,0;
mov ah,02;
mov al,01;
mov ch,01;
mov cl,01;
mov dh,01;
mov dl,00;
int 0x13;
}

for(i=0; i<(sectorsize * sectorct); i++)
{
fputc(memsectptr[i], outfile);
}
fclose(outfileprev);
fclose(outfile);
printf("\nreaching\n");
printf("reaching\n");
printf("reaching\n");
return(0);
Feb 14 '09 #13
JosAH
11,448 Expert 8TB
This is just a wild guess but try this:

Expand|Select|Wrap|Line Numbers
  1.     char * const memsectptr = (char *) 0x10000;
  2.  
Your assembly language snippet wants a segment of 0x1000; in those old 16 wide registers those segment registers were shifted to the left by four bits to form the actual address. Just a wild guess; your final address becomes 0x10000. I find that absolute address fiddling extremely tricky.

kind regards,

Jos
Feb 14 '09 #14
Jos,
After adding in the new value (0x10000) I was still obtaining the same results so to see what was happening with the pointer i added in some printf statements to read out the value of th pointer:

printf("\nmemsectptr = %c\n", memsectptr);
printf("memsectptr = %x\n", memsectptr);
printf("memsectptr = %d\n", memsectptr);

before on the hex value i was getting 1000 and on the decimal value 4096 but when i changed it to the new value it came out for both at zero?
Feb 14 '09 #15
JosAH
11,448 Expert 8TB
I bet your ints are two bytes wide. Can you print your pointer in %p format? To be sure cast the value 0x10000L (a trailing capital L) to a pointer instead.

kind regards,

Jos
Feb 14 '09 #16
Value for 0x1000 %p is 1000 whereas the 0x10000 is 0000 which does point to a 2 byte pointer. Can I rectify this?
Feb 14 '09 #17
JosAH
11,448 Expert 8TB
@dinklebaga
This is going to take ages; did you use 0x10000L (capital L at the end) for the initialization at the beginning of your code? Which memory model are you using? Isn't it possible to parameterize that piece of assembly? i.e. can you write 'mov bx, p' where p is a pointer value from your C code?

kind regards,

Jos
Feb 14 '09 #18
donbock
2,426 Expert 2GB
Can you put a global label in the assembly code at the start of the buffer? In my experience, an assembly language label of the form "_foo" was recognized as "foo" by the C compiler ... but your mileage may vary. You would need the proper declaration in your C module. Perhaps something like this ...
Expand|Select|Wrap|Line Numbers
  1. Assembly language:
  2. .globl _foo
  3. _foo:  .bss 4096
  4.  
  5. C language
  6. extern char foo;
  7.  
  8. memsectptr = &foo;
I have no idea what assembler you're using, so odds are my suggested assembly code is only notional.
Feb 14 '09 #19
@JosAH
Yes when initialising the value there was an L at the end of the memory location. I have been working on this last night and this morning and so far am still unable to determine the problem. I was able to parameterize the value going into the bx register (adding in memsectptr here) but still did not product the correct results. I am not too sure what you mean my the memory model. I am assuming that this is important but as I am still learning I haven't come across different ones yet.

Thanks,
Dinklebaga
Feb 15 '09 #20
Guys, firstly can I just say thankyou so much for your help it has been invaluable. I have now solved the problems encountered. The memory model that I was using before was small instead of large which I needed. This was a simple option changed when compiling the code using the Digital Mars C/C++ compiler using 16-bit DOS extender. For anybody wishing to do this in future the functions is: dmc filename.c -mld and the code that works with this is:
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* reads a given sector on the cylinder, head and drive (cyl, hd & dr)*/

read_sector(cyl, hd, dr)
char cyl, hd, dr;
{
int status;
int sectorsize = 512;
unsigned char far* memsectptr = (char unsigned far*) 0x10000000L;
int sectorct = 1;
int i;
FILE *outfile;
FILE *outfileprev;
outfileprev = fopen("hexdumpprev.txt", "wb");
outfile = fopen("hexdump.txt", "wb"); /*open 512 bytes of data in memory to read in sector */
for(i=0; i<(sectorsize); i++)
{
fputc(memsectptr[i], outfileprev); /*get memory values before use*/
}

asm
{
mov bx, 0x1000;
mov es,bx;
mov bx,0;
mov ah,02; /* BIOS read command */
mov al,01; /*read 1 sector */
mov ch,00; /*cylinder */
mov cl,01; /*start @ sector 1 */
mov dh,00; /*head */
mov dl,00; /*drive */
int 0x13; /*run code*/
}

for(i=0; i<(sectorsize * sectorct); i++)
{
fputc(memsectptr[i], outfile);
}
fclose(outfileprev);
fclose(outfile);
return(0);
}

verify_track(cyl, hd, dr)
char cyl, hd, dr;
{
int status;

_AH = 0x04;
_AL = 1;
_CH = cyl;
_CL = 1;
_DH = hd;
_DL = dr;
geninterrupt(0x13);
status = _AH;
return(status);
}

/* takes the status code returned by verify track and prints message associated with it */

pr_error(error)
int error;
{
switch(error)
{
case 0x01:
printf("Invalid Command\n");
break;
case 0x02:
printf("Address Mark Not Found\n");
break;
case 0x03:
printf("Disk Write Protected\n");
break;
case 0x04:
printf("Sector Not Found\n");
break;
case 0x05:
printf("Reset Failed\n");
break;
case 0x06:
printf("Floppy Disk Removed\n");
break;
case 0x08:
printf("DMA Overrun\n");
break;
case 0x09:
printf("DMA Crossed 64k Boundary\n");
break;
case 0x0C:
printf("Media Type Not Found\n");
break;
case 0x10:
printf("CRC Error\n");
break;
case 0x20:
printf("Controller Failed\n");
break;
case 0x40:
printf("Seek Failed\n");
break;
case 0x80:
printf("\n status %d\n", error);
}
}

/* main calls verify_track for all values of cyl (0-79) and both values for head (0-1). If function succeeds it returns 0
otherwise it returns the relevant error code to printf and terminates program.*/

main()
{
int status = 0;
char head, cylinder, sector;
char drive = 0;
for(cylinder = 0; cylinder < 79 && status==0; cylinder++)
{
for(head = 0; head < 2 && status==0; head++)
{
printf("Verifying head %d cylinder %02d\r", head, cylinder);
status = verify_track(cylinder, head, drive);
if (status != 0)
{
printf("\nError at head %d cylinder %02d :",head, cylinder);
pr_error(status);
return(0);
}
}
}
head = 0;
cylinder = 0;
status = read_sector(cylinder, head, drive);
if( status != 0)
{
printf("\nError at head %d, cylinder %02d :", head, cylinder);
pr_error(status);
}
else
{
printf("\nOperation completed successfully. See output\n");
}

}

Thanks once again,
Dinklebaga
Feb 15 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
0
by: Reece Hart | last post by:
Here's the basic issue: PostgreSQL doesn't use indexes unless a query criterion is of exactly the same type as the index type. This occurs even when a cast would enable the use of an index and...
3
by: /* frank */ | last post by:
Explicit conversion is made by mean of a cast i.e. float a; int b; .... b = (int) a; But the implicit conversion? What is?
2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
4
by: Hakirato | last post by:
Hi I have this compilation problem when trying to convert C code to C++. My function looks like this: void FTPServer_ThreadLoop( void *arg_p ) { FTPSettings_t* FTPSettings_p;
6
by: rCs | last post by:
How sound is the following advice for a C language coding standard? "Explicitly cast or convert variables. Do not rely on the implicit conversions." I have my opinions, but I would like to...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.