3일차에 마지막에 했던 예제문제, 

그 소스는 버리고 그냥 처음부터 다시 쳤다.

아직도 거기서 뭐가 문제였는지 모르겠음.

 

%include "io64.inc"

 

section .text

global CMAIN

CMAIN:

mov rbp, rsp; for correct debugging

;write your code here

mov ax, 0

mov bx, 0

 

;input

GET_DEC 1, al

GET_DEC 1, bl

 

mov [input_a], al

mov [input_b], bl

 

PRINT_DEC 1, input_a

PRINT_STRING str_space

PRINT_DEC 1, input_b

NEWLINE

 

;add

add al,bl

mov [result_add], al

 

 

;sub

mov al, [input_a]

sub al,bl

mov [result_sub], al

 

 

 

;mul

mov ax, 0

mov al, [input_a]

mul bx

mov [result_mul], ax

 

 

;div

mov ax,0

mov bx,0

 

mov al, [input_a];ax에 넣어서 계싼하면 오류는 안뜨나 뒤에 가 작동안함... 왜그런지 모르겠다.

mov bl, [input_b]

 

div bl

mov [result_div_quotient], al

mov bl, ah

mov [result_div_remainder], bl

 

 

;print

PRINT_STRING str_add

PRINT_DEC 1, result_add

NEWLINE

PRINT_STRING str_sub

PRINT_DEC 1, result_sub

NEWLINE

PRINT_STRING str_mul

PRINT_DEC 2, result_mul

NEWLINE

PRINT_STRING str_div_quotient

PRINT_DEC 1, result_div_quotient

NEWLINE

PRINT_STRING str_div_remainder

PRINT_DEC 1, result_div_remainder

NEWLINE

 

xor rax, rax

ret

 

section .bss

input_a resb 1

input_b resb 1

 

result_add resb 1

result_sub resb 1

result_mul resw 1

result_div_quotient resb 1

result_div_remainder resb 1

section .data

str_space db ' ',0x00

str_add db 'add : ',0x00

str_sub db 'sub : ',0x00

str_mul db 'mul : ',0x00

str_div_quotient db 'div-quotient : ',0x00

str_div_remainder db 'div-remainder : ',0x00

 

 

div연산이 2일차인가에 했던 예제랑 좀 다름

al,ah = ax / para (al에 몫, ah에 나머지)

여서 ax에 피제수를 넣고 para에 제수를 넣었는데

이상하게 ax에 피제수를 넣고 돌리면 프로그램이 멈춘다.

al에 피제수를 넣고 돌리면 ok~

이유는 모르겠음

 

 

 

shift 연산

 

section .text

global CMAIN

CMAIN:

;write your code here

mov ax, 0x1234

 

PRINT_HEX 2, ax

NEWLINE

 

shl ax,4

 

PRINT_HEX 2, ax

NEWLINE

 

mov [a], word 0x1234

PRINT_HEX 2, a

NEWLINE

 

shr word [a], 4 ; 메모리 변수의 크기를 말해주지않으면 에러...

PRINT_HEX 2,a

NEWLINE

 

 

xor rax, rax

ret

 

section .bss

a resw 1

 

and, or, xor, not 연산

include "io64.inc"

 

section .text

global CMAIN

CMAIN:

;write your code here

mov al, [target_a];0xb6

mov bl, 0b01010101; 0x55

 

;and

and al,bl

PRINT_HEX 1,al

NEWLINE

 

;or

mov al,[target_a]

or al,bl

PRINT_HEX 1,al

NEWLINE

;xor

mov al, [target_a]

xor al,bl

PRINT_HEX 1, al

NEWLINE

 

;not

mov al, [target_a]

not al

PRINT_HEX 1, al

NEWLINE

 

xor rax, rax

ret

 

section .data

target_a db 0b10110110; 0xb6 0b는 이진수

 

 

 

 

2byte 곱연산

%include "io64.inc"

 

section .text

global CMAIN

CMAIN:

;write your code here

;mov edx, 0

;mov eax, 0

 

mov ax, 0x2710;10000(10)

mov bx, 0x1388;5000(10)

 

mul bx ; dx:ax = ax*bx

shl edx, 16; dx에 이미 상위값이 들어가있다. 들어가기전에 왜 edx를0 으로 초기화를 안해주는걸까.?

and eax, 0x0000ffff;edx와 마찬가지로 ax에 하위값이 들어가있다.

or eax,edx

 

mov [result], eax

PRINT_HEX 4,result

NEWLINE

PRINT_DEC 4,result

NEWLINE

 

 

xor rax, rax

ret

 

section .bss

result resd 1

 

 

슬슬 소스좀 정리해서  깃에 넣어놔야할것 같음

'NASM' 카테고리의 다른 글

6일차.  (0) 2020.06.19
5일차.  (0) 2020.06.18
3일차.  (0) 2020.06.17
2일차  (0) 2020.06.15
1일차.  (0) 2020.06.14

+ Recent posts