카테고리

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/09

asm keyboard input

section .data
text1 db "what is you name? "
text2 db "Hello, "

section .bss
name resb 16

section .text
global _start

_start:
call _printText1
call _getName
call _printText2
call _printName

mov rax, 60
mov rdi, 0
syscall

_getName:
mov rax, 0
mov rdi, 0
mov rsi, name
mov rdx, 16
syscall
ret

_printText1:
mov rax, 1
mov rdi, 1
mov rsi, text1
mov rdx, 18
syscall
ret

_printText2:
mov rax, 1
mov rdi, 1
mov rsi, text2
mov rdx, 7
syscall
ret

_printName:
mov rax, 1
mov rdi, 1
mov rsi, name
mov rdx, 16
syscall
ret

; compile : nasm -f elf64 input_hello.asm -o input_hello.o
; ld input_hello.o -o input_hello

asm 이동, 호출, 비교

1. Flags

    • 플래그는 레지스터와 마찬가지로 데이터를 보유한다.
    • 플래그는 1 비트 씩만 보유한다. false 또는 True
    • 개별 플래그는 더 큰 레지스터의 일부임.

Flag Symbol Description
CF Carry
PF Parity
ZF Zero
SF Sign
OF Overflow
AF Adjust
IF Interrupt Enabled

2. Pointers
    • 포인터 레지스터 또한 데이터를 보유한다.
    • 포인터가 데이터를 가리키면, 메모리 주소를 보유한다는 것을 의미한다.

Pointer Name Meaning Description
rip (eip, ip) Index pointer 제어 흐름에서 실행될 다음 주소를 가리킴.
Rrsp (esp, sp) Stack pointer 스택의 최상위 주소 가리킴
Rrbp (ebp, bp) Stack base pointer 스택의 맨 아래를 가리킴
...
3. Control Flow
    • 모든 코드는 기본적으로 위에서 아래로 실행된다. 프로그램이 흐르는 방향을 제어 흐름이라고 한다.
    • 추출 레지스터 rip는 실행될 다음 명령어의 주소를 보유한다. 각 명령 후, 제어 흐름이 위에서 자연스럽게 흐르도록 1 씩 증가한다.

4. Jump
    • jump를 사용하면 레이블 기반으로 다른 코드 부분으로 이동할 수 있다.
    • jump는 프로그램 흐름을 전환하는데 사용한다.
    • jmp lable <- rip 레지스터에 "label" 값으로 로드한다.

5. Comparisons
    • 비교를 통해 프로그램을 특정 조건에 따라 다른 경로를 취할 수 있다.
    • 비교는 레지스터에서 수행.
    • 비교 일반적 형식

cmp register, register/value
cmp rax, 23
cmp rax, rbx

5.1 Comparisons with Flags
    • 비교가 이루어지면 특정 플래그가 설정된다.

cmp a, b
a=b
ZF = 1
A != b
ZF = 0
-
SF = msb(a-b
...
...

5.2 Conditional Jumps
    • 비교가 이루어지면 조건부 점프를 할 수 있다.
    • 코드의 조건부 점프는 jump 작성 되며, jmp로 대체할 수 있다.

Jump symbol(signed)
Jump symbol(unsigned)
Results of cmp a,b
je
-
a = b
jne
-
a != b
jg
ja
a > b
jge
jae
a >= b
ji
jb
a < b
jle
jbe
a <= b
jz
-
a = 0
jnz
-
a != 0
jo
-
Overflow occurred
jno
-
Overflow did not occur
js
-
jump if signed
jns
-
jump if not signed
    • 아래 코드 rax 레지스터의 값이 23인 경우에만 “_doThis” 레이블의 주소로 이동하다.
cmp rax, 23
je _doThis

    • 아래 코드 rax 레지스터의 값이 rbx 레지스트의 값보다 큰 경우에만 레이블 “_doThis”의 주소로 이동한다.
cmp rax, rbx
jg _doThis

6. Registers as Pointers
기본 레지스터는 포인터로 처리 될 수 있다.
레지스터를 포인터로 처리하려면 “rax”가 “[rax]” 되도록 레지스터 이름을 대괄호로 묶는다.

mov rax, rbx
rbx 레지스터의 값을 rax 레지스터에 로드한다.

mov rax, rbx
rbx 레지스터의 값을 rax 레지스터에 로드한다.

mov rax, [rbx]
rbx 레지스터가 가리키는 값을 rax 레지스터에 로드한다.

7. Calls
    • 호출은 점프와 본질적 동일한 모양을 가진다.
    • “call”을 사용하면 호출이 이루어진 원래 위치는 “ret”를 사용해 반환한다.
    • print 코드 부분 “Hello, World” 자체 섹션으로 이동 후 해당 섹션이 호출 된다.
    • 이를 서부 루틴 이라 한다.

C 구조체 재 사용 설정 추가 위치 접근

#include <stdio.h>

struct catsFavs{
char *food;
char *friend;
};
typedef struct cat{
const char *name;
const char *breed;
int avgHeightCm;
int avgWeightLbs;

struct catsFavs favoriteThings;
} cat;

void getCatFavs(cat theCat){
printf("\n");

printf("%s loves %s and his friend is %s\n\n",
theCat.name,
theCat.favoriteThings.food,
theCat.favoriteThings.friend);
}

void setCatWeightPtr(cat *theCat, int newWeight){
(*theCat).avgWeightLbs = newWeight;
printf("The weight was changed to %d\n\n", (*theCat).avgWeightLbs);
printf("The weight was changed to %d\n\n", theCat->avgWeightLbs);
}

void main(void){
cat juju = {"juju", "persian", 25, 9, {"meat", "joe camp"}};


setCatWeightPtr(&juju, 11);

printf("The Weight in Main() %d\n\n", juju.avgWeightLbs);
}

C 구조체 재 추가 사용 설정

#include <stdio.h>

struct catsFavs {
char *food;
char *friend;
};
typedef struct cat {
const char *name;
const char *breed;
int avaHeightCm;
int avgWeightLbs;

struct catsFavs favoriteThings;

} cat;

void getCatFavs(cat theCat){
printf("\n");

printf("%s loves %s and his friend is %s\n\n",
theCat.name,
theCat.favoriteThings.food,
theCat.favoriteThings.friend);
}

void setCatWeight(cat theCat, int newWeight){
theCat.avgWeightLbs = newWeight;

printf("The weight was changed to %d\n\n", theCat.avgWeightLbs);
}

void main(void){

cat juju = {"Juju", "Persian", 25, 9, {"meat", "joe Camp"}};

getCatFavs(juju);

setCatWeight(juju, 11);

printf("The Weight in Main() %d\n\n", juju.avgWeightLbs);


}

C 구조체 선언 추가

#include <stdio.h>

struct catsFavs {
char *food;
char *friend;
};
typedef struct cat {
const char *name;
const char *breed;
int abaHeightCm;
int abgWeightLbs;

struct catsFavs favoriteThings;

} cat;

void getCatFavs(cat theCat){
printf("\n");

printf("%s loves %s and his friend is %s\n\n",
theCat.name,
theCat.favoriteThings.food,
theCat.favoriteThings.friend);
}

void main(void){

cat juju = {"Juju", "Persian", 25, 9, {"meat", "joe Camp"}};

getCatFavs(juju);
}

C 구조체 자원 재사용 및 메모리 무작위 위치 확인

#include <stdio.h>

struct cat {
const char *name;
const char *breed;
int avgHeightCm;
int avgWeightLbs;

};

void getCatInfo(struct cat theCat){
printf("\n");

printf("Name: %s\n\n", theCat.name);
printf("Breed: %s\n\n", theCat.breed);
printf("Avg Height: %d cm\n\n", theCat.avgHeightCm);
printf("Avg Weight: %d lbs\n\n", theCat.avgWeightLbs);
}

void getMemoryLocations(struct cat theCat){
printf("Name Location: %s\n\n", theCat.name);
printf("Breed Location: %s\n\n", theCat.breed);
printf("Height Location: %d\n\n", &theCat.avgHeightCm);
printf("Weight Location: %d\n\n", &theCat.avgWeightLbs);
}

void main(void){
struct cat juju = {"Juju", "Persian", 45, 50};

getCatInfo(juju);

struct cat juju2 = juju;
getMemoryLocations(juju);
getMemoryLocations(juju2);
}

C 구조체 선언 및 호출

#include <stdio.h>

struct cat {
const char *name;
const char *breed;
int avgHeightCm;
int avgWeightLbs;
};

void getCatInfo(struct cat theCat){
printf("\n");

printf("Name: %s\n\n", theCat.name);
printf("Breed: %s\n\n", theCat.breed);
printf("Avg Height: %d cm\n\n", theCat.avgHeightCm);
printf("Avg Weight: %d lbs\n\n", theCat.avgWeightLbs);
}

void main(void){
struct cat juju = {"Juju", "persian", 45, 132};

getCatInfo(juju);

}

C 구조체 선언

#include <stdio.h>

struct cat {
const char *name;
const char *breed;
int avgHeightCm;
int avgWeightLbs;
};

int main(void){
struct cat juju = {"Juju", "Persian", 30, 164};

printf("name = %s\n\n", juju.name);
printf("species %s\n\n", juju.breed);
printf("Height = %dcm\n\n", juju.avgHeightCm);
printf("Weight = %dLb\n\n", juju.avgWeightLbs);
}

C key value 에서 value 변경.

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

void generateTwoRandomNums(int random1, int random2){
random1 = rand() % 50 + 1;
random2 = rand() % 50 + 1;

printf("New random1 in function = %d\n\n", random1);
printf("New random2 in function = %d\n\n", random2);
}

void pointerRandomNumbers(int* random1, int* random2){
*random1 = rand() % 50 + 1;
*random2 = rand() % 50 + 1;

printf("New random1 in pointer function = %d\n\n", *random1);
printf("New random2 in pointer function = %d\n\n", *random2);
}

void main(void){
int random1 = 0, random2 = 0;

generateTwoRandomNums(random1, random2);

printf("random1 = %d\n\n", random1);
printf("random2 = %d\n\n", random2);

random1 = 0, random2 = 0;
printf("Main Before Function Call\n\n");
printf("random1 = %d : random2 = %d\n\n", random1, random2);

pointerRandomNumbers(&random1, &random2);
printf("Main After Function Call\n\n");
printf("random1 = %d : random2 = %d\n\n", random1, random2);
}

C key value

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

void generateTwoRandomNums(int random1, int random2){
random1 = rand() % 50 + 1;
random2 = rand() % 50 + 1;

printf("New random1 in function = %d\n\n", random1);
printf("New random2 in function = %d\n\n", random2);
}

void pointerRandomNumbers(int* random1, int* random2){
*random1 = rand() % 50 + 1;
*random2 = rand() % 50 + 1;

printf("New random1 in pointer function = %d\n\n", *random1);
printf("New random2 in pointer function = %d\n\n", *random2);
}

void main(void){
int random1 = 0, random2 = 0;

generateTwoRandomNums(random1, random2);

printf("random1 = %d\n\n", random1);
printf("random2 = %d\n\n", random2);

random1 = 0, random2 = 0;
printf("Main Before Function Call\n\n");
printf("random1 = %d : random2 = %d\n\n", random1, random2);

pointerRandomNumbers(&random1, &random2);
printf("Main After Function Call\n\n");
printf("random1 = %d : random2 = %d\n\n", random1, random2);
}

C 배열 선언 후 무작위 메모리 위치 값 확인(즉 2차 정보(목차) 생성)

#include <stdio.h>

void main(void){
int random1 = 23, random2 = 27;

printf("random1 = %p : random2 = %p\n\n", &random1, &random2);
printf("Size of int %ld\n\n", sizeof(int));

int * pRandom1 = &random1;

printf("Pointer %p\n\n", pRandom1);
printf("Value %ld\n\n", pRandom1);
printf("Value %d\n\n", *pRandom1);

int primeNumbers[] = {2,3,5,7};
printf("First index : %d\n\n", primeNumbers[0]);
printf("First index with * : %d\n\n", *primeNumbers);
printf("Second index with * : %d\n\n", *(primeNumbers + 1));

char * students[4] = {"Lee", "hyung", "young", "tae"};
for(int i = 0; i < 4; i++){
printf("key=%s : memory value=%ld\n\n", students[i], &students[i]);
}
}

C 무작위 할당된 메모리 위치 확인

#include <stdio.h>

void main(void){
int random1 = 18, random2 = 19;

printf("randl = %p\n\n : random2 = %p\n\n", &random1, &random2);
printf("Size of int %ld\n\n", sizeof(int));

int * pRandom1 = &random1;
printf("Pointer %p\n\n", pRandom1);
printf("Value %ld\n\n", pRandom1);
printf("Value %d\n\n", *pRandom1);
}

C 메모리 자원 무작위 할당 확인.

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

void main(void){
int random1 = 12, random2 = 15;

printf("random1 = %p : random2 = %p\n\n", &random1, &random2);
printf("random1 = %d : random2 = %d\n\n", &random1, &random2);
printf("Size of int %ld\n\n", sizeof(random1));
}