I do not need the C code, but can you please tell me how to do the 4 phases when
ID: 3890224 • Letter: I
Question
I do not need the C code, but can you please tell me how to do the 4 phases when i compile and how do i get the hexadecimal format of the code?
5. C programming Write a simple C program similar to the Welcome.c code we showed in the class, but the difference is that the message should be "Welcome to CIS2107, your name", where "your name" should be your first name+last name. Submit a copy of the output of the hexadecimal format of your program using hexdump -C. Also submit a screenshot of the 4 phases when you compile this program in steps.Explanation / Answer
//Below is the C code file name is test.c:
#include<stdio.h>
int main ()
{
char name[]="first last";
printf("Welcome to CIS2107, %s ",name);
return 0;
}
Four Phases are:
1) gcc -E test.c
above option E will generate the preprocessed code like below .
:
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/bits/libc-header-start.h" 1 3 4
# 33 "/usr/include/bits/libc-header-start.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 410 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 441 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 442 "/usr/include/sys/cdefs.h" 2 3 4
......
2) gcc -S test.c
above command generate the assembly code in file test.s (please fins the attached file).
.file "test.c"
.section .rodata
.LC0:
.string "Welcome to CIS2107, %s "
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movabsq $7020021603688933734, %rax
movq %rax, -11(%rbp)
movw $29811, -3(%rbp)
movb $0, -1(%rbp)
leaq -11(%rbp), %rax
movq %rax, %rsi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 7.1.1 20170622 (Red Hat 7.1.1-3)"
.section .note.GNU-stack,"",@progbits
3) gcc -c test.c
above command will compile the c program and output the test.o file
4) hexdump -C test.c
above command will give the hexdump for c code like below:
00000000 23 69 6e 63 6c 75 64 65 3c 73 74 64 69 6f 2e 68 |#include<stdio.h|
00000010 3e 0d 0a 0d 0a 69 6e 74 20 6d 61 69 6e 20 28 29 |>....int main ()|
00000020 0d 0a 7b 0d 0a 0d 0a 09 63 68 61 72 20 6e 61 6d |..{.....char nam|
00000030 65 5b 5d 3d 22 66 69 72 73 74 20 6c 61 73 74 22 |e[]="first last"|
00000040 3b 0d 0a 09 70 72 69 6e 74 66 28 22 57 65 6c 63 |;...printf("Welc|
00000050 6f 6d 65 20 74 6f 20 43 49 53 32 31 30 37 2c 20 |ome to CIS2107, |
00000060 25 73 20 5c 6e 22 2c 6e 61 6d 65 29 3b 0d 0a 20 |%s ",name);.. |
00000070 20 20 20 72 65 74 75 72 6e 20 30 3b 0d 0a 7d 0d | return 0;..}.|
00000080 0a |.|
00000081
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.