invalid suffix "disassemble" on integer constant - c

I'm writing an Intel 8080 Disassembler when I came across this problem.
Here's the code:
int 8080disassemble(unsigned char *cbuffer, int pc){
unsigned char* code=&cbuffer[pc];
int opbytes=1;
printf("%04x ",pc);
switch(*code){
case 0x00:printf("NOP");
break;
case 0x01:printf("LXI B,#$%02x%02x",code[2],code[1]);opbytes=3;
break;
case 0x02:printf("STAX B");
break;
case 0x03:printf("INX B");
break;
case 0x04:printf("INR B");
break;
case 0x05:printf("DCR B");
break;
case 0x06:printf("MVI B,#$%02x",code[1]);opbytes=2;
break;
}
}
It's a header file, that's why there is no includings.
Thanks!
I tried making it a pointer but didn't work

Identifiers in C (such as functions and variable names) are not allowed to start with a digit. The compiler is trying to parse 8080disassemble as a number instead, and gets confused when it reaches the d.
Choose a different name for your function, such as perhaps cpu_8080_disassemble or disassemble_8080.

Related

Please explain colons within optional arguments and optarg as seen in the code below

All of the code below makes sense with the exception of how it is that colons are being used and optarg function as seen under case 's' and 'f'. I've attempted to google what exactly each means but I get lost in the jungle of computer science language that confuses me.
while ((c = getopt(argc, argv, "Ss:f:")) != -1) { //ask about colons in office hours
switch (c)
{
case 'S':
should_print_file_size = 1;
break;
case 's':
min_file_size = atoi(optarg);
break;
case 'f':
substring = optarg;
break;
default:
printf("Please only use S, s, or f arguments.");
return -1;
}
}
Please help!
I am assuming you mean this getopt in the while:
while ((c = getopt(argc, argv, "Ss:f:")) != -1)
As #Kaylum pointed out in the comments, the getopt manual states the following syntax:
int getopt(int argc, char * const argv[],
const char *optstring);
Where optstring argument is described as:
optstring is a string containing the legitimate option characters. If
such a character is followed by a colon, the option requires an
argument, so getopt() places a pointer to the following text in the
same argv-element, or the text of the following argv-element, in
optarg
So you can see in the following two cases for s and f:
case 's':
min_file_size = atoi(optarg); // <--- case 's' Takes optarg
break;
case 'f':
substring = optarg; // <--- case'f' takes optarg
break;
and from the while you have s: and f:. So imagine it being stated as something like this:
s: <argument> and f: <argument>

Why I have to do c-48? and what does \a and %2.f mean? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
This programm is a simple calculater.
At first sorry for my bad english, i want wo know whats the meaning of '\a' & '%2.f'
I know %f is for double but why '2.' in front of 'f'?
Right before the end we set 'dgt' to 'c-48' i want to know why '-48'
I am a informatic student and I am at the beginning, you have some special tips for me?
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
printf("Simple Calculator: \nValid inputs are +-*/=and digits 0,...,9\n");
printf("Your input: \n");
double r = 0.0;
double dgt = 0.0;
char lst_opt = '+';
const int nxt_dgt = 1;
const int nxt_opt = 2;
int nxt_npt = nxt_dgt;
while (1)
{
char c = _getch();
switch (c)
{
case '+':
case '-':
case '*':
case '/':
if (nxt_npt != nxt_opt)
{
printf("\a");
break;
}
printf("%c", c);
lst_opt = c;
nxt_npt = nxt_dgt;
break;
case '=':
if (nxt_npt != nxt_opt)
{
printf("\a");
break;
}
printf("\n=%.2f", r); //double, but why .2?
nxt_npt = nxt_opt;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (nxt_npt != nxt_dgt)
{
printf("\a");
break;
}
printf("%c", c);
dgt = c-48;
switch (lst_opt)
{
case '+': r += dgt; break;
case '-': r -= dgt; break;
case '*': r *= dgt; break;
case '/': r /= dgt; break;
}
nxt_npt = nxt_opt;
break;
}
}
_getch();
}
What does \a means?
Like \n, an escape character meaning "newline", it is an escape character too, meaning "alert", which makes a beep sound when "printed".
You can find informations (including other special escape characters) Here.
Why minus 48?
In the code, it is trying to get a integer from a character. Instead of doing things like if(c == '3') i = 3;, it uses how the character '0'~'9' are ordered in the ACSII Table. You can see that, '0'~'9' are orded tightly from 48 to 57. that is, '0' - 48 produces a result of 0, and the same for other ones.
%2.f
You know %f are used for printing a float variable, that's good, however there is more to know. You are able to format how the variables are going to be printed.
The 2. stands for "Print the float number at least 2 character wide, also none
of the decimal places should be printed". More printf() formatting informations Here.
whats the meaning of '\a'
This one is actually somewhat obscure. The escape sequence \a appearing in a character constant or string literal represents a character that, when delivered to a terminal, causes an alert signal to be sounded and / or displayed without changing the output position. Details of the signal are environment specific, but it might produce a beep or cause the screen to flash, or similar. This is rarely used these days.
& '%2.f' I know %f is for double but why '2.' in front of 'f'?
For information about printf format strings, you would do well to look up good reference material -- online manual pages (search keywords "man printf") would be my recommendation, though the language specification documents are the ultimate authority. Textbooks and tutorials do not typically cover all the details, which are many.
If you did that, you would find that the 2 is a (minimum) field width, and the . is shorthand for .0, a precision specification instructing printf to emit zero fractional digits.
Right before the end we set 'dgt' to 'c-48' i want to know why '-48'
It is essential to understand that there is a difference between digit characters and the numbers they represent. 48 is the ASCII code for the character '0', and C requires each subsequent decimal digit to have a code one greater than the previous, so, supposing that the implementation uses ASCII-compatible character codes, dgt-48 converts any decimal digit character to its corresponding numeric value.
However, this would be more safely and more idiomatically expressed as dgt - '0'. That's clearer, in that it at least provides a clue about what's going on, and it works even on implementations that use character encodings that are not ASCII compatible.
\a is use to make a sound
c-48 : as stated in the comment, 48 is the ascii value of character ´0’. So c-48 convert ascii character ‘0’ .. ´9’ to integer value 0 ... 9
In printed documentation you could see that %2.f means that you print a floating value with at least 2 characters at the left of decimal point and no decimal eg :
2.643 -> ´ 2’
123.4 -> ´123’

c optarg atoi with no args

Consider the following code:
int number;
while((w = getopt(argc, argv, "n:s:")) != -1) {
switch (w){
case 'n': {
opfile->filename = optarg;
}break;
case 's': {
number = atoi(optarg);
}break;
}
}
Now, when I leave both options or the option s blank, for example I start my program with no command line args, then the number variable still gets a random value.
What am I missing here? Some if-statement in the case of s? Specifically, I want to cover the case where the user doesn't assign a specific value/option to s in the command line arguments.
When there is no 's' option passed to the program, the case 's' branch is not executed at all, and nothing else sets number to a value, which means that subsequent reads trigger undefined behavior. (This is potentially much worse than just giving you a random value when you read from it later. It's a must-fix bug.)
But because nothing else touches number, it will be enough to change
int number;
to
int number = 0;
or whatever else you want your default to be.
(By the way, you should really be using strtol instead of atoi, because atoi ignores syntax errors.)

Switch Case working differently

Consider two codes. Why are they giving different outputs though same value hass been assigned to i ,'i' being a char in both the codes.
first code-->
(here value is assigned to i directly)
void main()
{
char i=3;
clrscr();
switch(i)
{
default : printf("\nHi..\n");
break;
case 1:printf("\na");
break;
case 2:printf("\nb\n");
break;
case 3:printf("\nc");
break;
}
}
second using printf-scanf--->
void main()
{
char i;
printf("ENTER i");
scanf("%c",&i);
clrscr();
switch(i)
{
default : printf("\nHi..\n");
break;
case 1:printf("\n\na");
break;
case 2:printf("\nb\n");
break;
case 3:printf("\nc");
break;
}
}
in the second code when i m giving 3 as input, i get "Hi.." as output. What makes the two codes work differently..??
In the first you're using
char i = 3
But when you use scanf you essentially use:
char i = '3'
These two contain different values
See the following ASCII http://www.asciitable.com/
After reading the character from stdin:
scanf("%c",&i);
i will contain the ASCII code of 3 (51) and not the value 3, leading to taking the default branch of switch.
The solution is declaring your variable as int and using
scanf("%d",&i);
to read it.
In the first example you assigned to i the integer value 3, which is not the same as assigning the character '3'. In C, when you assign a value to a char variable that value would represent a code specific to a certain character (considering a standard).
In the second example the scanf function read a character from stdin, which was interpreted as a character due to the use of %c, and assigned to the given variable the code specific to the read character.
It is not related to the switch statement, but to the scanf function.
Read its documentation ie scanf(3) man page. See also this answer to a very related question.
Notice that the char '3' is not encoded as 3, but as 51 in ASCII
Learn to enable all warnings and debugging info (e.g. compile using gcc -Wall -g) and learn to use the debugger (i.e. gdb)
Change your code to
char i -> int i;
scanf("%c",&i) -> scanf(" %d",&i);

Pass multiple arguments in C command line

How am i able to pass multiple arguments in a C program like this, using different switches
program -d <argument1> -p <argument2>
I'm using getopt to enable me to pass arguments.
int main(int argc, char **argv)
{
while(1)
{
unsigned int c = getopt(argc, argv, "-dD:hHgGp:");
if( c == -1 ) break;
switch( c )
{
case 'D':
case 'd':
printf("\nd=");
strcpy(D,optarg);
printf(D);
break;
case 'g':
case 'G':
printf("g");
break;
case 'p':
printf("\nPath=");
strcpy(pathFile,optarg);
printf(pathFile);
break;
case 'H':
case 'h':
usage(); //For help
return 0;
default:
return 0;
}
}
}
EDIT: The code here is a dummy code which I use for testing. It returns the argument that is passed as a string.
Is it just a case of you forgetting the “:” after the “d” in getopt arguments?
unsigned int c = getopt(argc, argv, "-d:D:hHgGp:");
It seems rather odd to write this:
while (1)
{
unsigned int c = getopt(argc, argv, "-dD:hHgGp:");
if( c == -1 ) break;
The return value of getopt() is an int; why would you save it in an unsigned int?
int opt;
while ((opt = getopt(argc, argv, "-dD:hHgGp:")) != -1)
{
switch (opt)
{
case ...
}
}
If you're going to make options case-insensitive (not a good idea, IMO), then be consistent about it and handle P: too. Also, as first noted by kmkaplan's answer, you have D: and d being handled by the same switch; they should both be followed by a colon for sanity's sake: "-d:D:hHgGp:P:" would at least be self-consistent.
Also, under most circumstances, you don't need to copy the argument string (optarg) anywhere; you simply save a pointer to its current value in a convenient variable. If you do copy the argument string, you must check the length of the argument to ensure you are not overflowing buffers.
The first character of the option string is not normally a dash; it isn't a standard behaviour. The Mac OS X documentation for getopt() does note that it is a GNU extension and advises against ever starting an option string with a dash (and the option string should only contain a dash for backwards compatibility, not in new code — again, on Mac OS X or BSD). Under GNU getopt(), the leading dash means that non-option arguments are reported as if they were options. As long as you're aware that you're using a GNU getopt() extension, there's no harm in doing so.

Resources