카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2018/12/28

asm 서브루틴

section .data
text db "Hello, World!",10,0
text2 db "World?",10,0

Section .text
global _start

_start:
mov rax, text
call _print

mov rax, text2
call _print

mov rax, 60
mov rdi, 0
syscall

;input: rax as pointer to string
;output: print string at rax
_print:
push rax
mov rbx, 0
_printLoop:
inc rax
inc rbx
mov cl,  [rax]
cmp cl, 0
jne _printLoop

mov rax, 1
mov rdi, 1
pop rsi
mov rdx, rbx
syscall

ret

; nasm -f elf64 05_SubRoutine.asm -o 05_SubRoutine.o
; ld 05_SubRoutine.o -o 05_SubRoutine
; ./05_SubRoutine

구조체 malloc() 사용법.

#include <stdio.h>
#include <stdlib.h>

struct product{
float price;
char productName[30];
};

int main(void){
struct product *pProducts;

int i, j;

int numberOfProducts;
printf("Enter the Number of products to store: ");

scanf("%d", &numberOfProducts);
pProducts = (struct product *) malloc(numberOfProducts * sizeof(struct product));

for(i=0; i < numberOfProducts; ++i){
printf("Enter a Product Name: ");
scanf("%s", &(pProducts+i)->productName);

printf("Enter a Product Price: ");
scanf("%e", &(pProducts+i)->price);

}

printf("Products Stored\n");

for(j=0; j < numberOfProducts; ++j){
printf("%s\t%.2f\n", (pProducts+j)->productName, (pProducts+j)->price);
}

free(pProducts);

return 0;
}

/*
 * ./02_malloc_struct_store
Enter the Number of products to store: 3
Enter a Product Name: Egg
Enter a Product Price: .25
Enter a Product Name: Fish
Enter a Product Price: 11
Enter a Product Name: Bread
Enter a Product Price: 3
Products Stored
Egg 0.25
Fish 11.00
Bread 3.00 */

변수 malloc 사용법.

#include <stdio.h>
#include <stdlib.h>

int main(void){
int amtOfNumbersToStore;

printf("How many numbers do you want to store: ");

scanf("%d", &amtOfNumbersToStore);

int * pRandomNumbers;

pRandomNumbers = (int *) malloc(amtOfNumbersToStore * sizeof(int));

if(pRandomNumbers != NULL){
int i = 0;

printf("Enter a Number of Quit: ");

while(i < amtOfNumbersToStore && scanf("%d", &pRandomNumbers[i]) == 1) {

printf("Enter a number or Quit: ");
i++;
}
printf("\nYou entered the following numbers\n");

for(int j=0; j < i; j++){
printf("%d\n", pRandomNumbers[j]);
}
}

free(pRandomNumbers);

// 10k 더이상 사용되지 않지만 메모리 해제 안함
// 10k 더이상 사용되지 않지만 메모리 해제 안함
// 10k 더이상 사용되지 않지만 메모리 해제 안함

return 0;

}

/*
./01_number_store
How many numbers do you want to store: 10
Enter a Number of Quit: 1
Enter a number or Quit: 2
Enter a number or Quit: 3
Enter a number or Quit: 4
Enter a number or Quit: 5
Enter a number or Quit: q

You entered the following numbers
1
2
3
4
5
*/

문자 정보 확인

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

void noMoreNewline(char* theString){
char * isANewline;

isANewline = strrchr(theString, '\n');

// 개형문자
if(isANewline){
*isANewline = '\0';
}
}

void makeLowercase(char* theString){
int i = 0;

while(theString[i]){
// 소문자 변경
theString[i] = tolower(theString[i]);
// 대문자 변경
// theString[i] = toupper(theString[i]);
i++;
}
}

void getCharInfo(){
char theChar;

while((theChar = getchar()) != '\n'){
printf("Letter of Number %d\n\n", isalnum(theChar));

printf("Alphabetic Char %d\n\n", isalpha(theChar));

printf("Standard Blank %d\n\n", isblank(theChar));

printf("Ctrl Char %d\n\n", iscntrl(theChar));

printf("Number Char %d\n\n", isdigit(theChar));

printf("Anything But space %d\n\n", isgraph(theChar));

printf("Lowercase %d\n\n", islower(theChar));

printf("Uppercase %d\n\n", isupper(theChar));

printf("Punctuation %d\n\n", ispunct(theChar));

printf("Any Space %d\n\n", isspace(theChar));
}
}

int main(void){
getCharInfo();
}

개형문자, 대소문자

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

void noMoreNewline(char* theString){
char * isANewline;

isANewline = strrchr(theString, '\n');

// 개형문자
if(isANewline){
*isANewline = '\0';
}
}

void makeLowercase(char* theString){
int i = 0;

while(theString[i]){
// 소문자 변경
theString[i] = tolower(theString[i]);
// 대문자 변경
// theString[i] = toupper(theString[i]);
i++;
}
}

int main(void){
char doYouWantToQuit[10];

printf("Enter quit to quit: ");

fgets(doYouWantToQuit, 10, stdin);

noMoreNewline(doYouWantToQuit);

makeLowercase(doYouWantToQuit);

printf(doYouWantToQuit);

while(strcmp(doYouWantToQuit, "quit")){
printf("Enter quit to quit: ");
fgets(doYouWantToQuit, 10, stdin);
noMoreNewline(doYouWantToQuit);
makeLowercase(doYouWantToQuit);

}

printf("Thank you for typing quit %s\n\n", doYouWantToQuit);

}

개행 문자.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

void noMoreNewline(char* theString){
char * isANewline;

isANewline = strrchr(theString, '\n');

if(isANewline){
*isANewline = '\0';
}
}
int main(void){
char doYouWantToQuit[10];

printf("Enter quit to quit: ");

fgets(doYouWantToQuit, 10, stdin);

noMoreNewline(doYouWantToQuit);

printf(doYouWantToQuit);
}

putchar() 사용 예

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main(void){
char * randomString = "Just some random stuff";

#if 0 // 간접 연산자 지정 접근
while(*randomString){
putchar(*randomString++);
}
#endif

int i = 0;

// 배열 접근
while(randomString[i] != '\0'){
putchar(randomString[i++]);
}
}

fgets(), fputs() 사용 예

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main(void){
char name[50];

printf("What is your name? ");

fgets(name, 50, stdin);

fputs("Hi ", stdout);
fputs(name, stdout);
}

gets(), puts() 사용 예.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main(void){
char name[50];
printf("What is your name? ");
gets(name);
puts("Hi");
puts(name);
}

getchar() putchar() 사용 예

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main(void){
char theChar;

while((theChar = getchar()) != '~'){
putchar(theChar);
}
}

숫자의 합

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main(void){

// bool isAnumber;
_Bool isANumber;

int number;
int sumOfNumbers = 0;

printf("Enter a Number: ");

isANumber = (scanf("%d", &number) == 1);

while(isANumber) {
sumO Numbers = sumOfNumbers + number;
printf("Enter a Number: ");
isANumber = (scanf("%d", &number) == 1);
}

printf("The Sum is %d\n\n", sumOfNumbers);
}