The operating system is Ubuntu 18.04
hello.c
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; }
syscall-hello.c
#include <unistd.h> #include <sys/syscall.h> char *buf = "Hello world!\n"; int main(int argc, char *argv) { size_t result; /* "man 2 write" to see arguments to write syscall */ result = syscall(SYS_write, 1, buf, 13); return (int) result; }
A machine instruction that:
– Increases the CPU privilege level and
– Passes the control to a predefined place in the kernel.
● Arguments are (typically) passed in CPU registers.
● Instructions: – x86: int 0x80, sysenter, syscall
– MIPS: syscall
– ARM: swi
hello.c
#include <stdio.h>
int main (int argc, char *argv[])
{
printf(“Hello world\n”);
}
Let's look how to do it without libc write system call
– Documentation: man 2 write
– ssize_t write(int fd, const void *buf, size_t count);
– Three arguments
– startup symbol
hello:
.ascii "Hello world\n"
.global _start
_start:
mov $4,%eax # write
mov $1,%ebx # stdout
mov $hello,%ecx # ptr to data
mov $12,%edx # length of data
int $0x80
Compile: gcc -m32 -nostdlib -o hello1 hello1.s
Run: ./hello1
Why it ends with segmentation fault? Disassemble the binary: objdump -d hello1
hello:
.ascii "Hello world\n"
.global _start
_start:
mov mov mov mov |
$4,%eax $1,%ebx $hello,%ecx $12,%edx |
# # # # |
write stdout ptr to data length of the |
int |
$0x80 |
||
mov |
$1,%eax |
# |
exit |
mov int |
$0,%ebx $0x80 |
# |
exit code |
We need to tell the OS that we are about to finish – with exit syscall.
Inspect the syscalls invoked by the program:
strace ./hello > /dev/null
The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello...
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function
Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...
#include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...
include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf(" void printbinaryfunsigned char x) int main (int argc, char argvl) r mask atoi(argv[1): unsigned char maskatoi(argv[11): ensigned ahao data - ateilargvl21): char operation if argc 3 printf(" printf" n" printf("usage: %s FristArg SecondArgin", argle)); return;
#include <stdio.h> int main(int argc, char *argv[]) { char a, *pc, c[9]; int i, *pk, k[9]; a='z'; pc=&(c[8]); pk=&(k[0]); for (i=0; i<9; i++) { *pc=a-(char)i; pc--; *pk=(int)a-i; pk++; } return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...
#include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...
PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...
OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...
main.c #include <stdio.h> int main() { Tong Tong int x = 1234567890000; char y = (char) x; printf("%d\n", y); Compile and run the above main.c file. a) Indicate the compiler command you would use to compile this program. Do any of the following flags affect the compilation? -Wall -Werror -pedantic -Woverflow b) Indicate any discrepancy between expected and actual behavior, and as a programmer, how to construct best practices to avoid this deviation. c) What happens if we do not...
What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...