1. 참거짓 문제 4문항
2. 스텍을 Push를 대체(실제로 수행되는)할 수 있는 코드
add esp, 4
mov[esp], eax
3. 코드 실행 후 esi 값
4. 레지스트 값 (연습문제로 충분히 가능)
5. 코드 빈칸에 들어 갈 것
정답이 아마 배열의 자료형를 표현하는 코드 (TYPE array) 인것으로 기억남...
6. C++을 어셈블리언어로 바꾸는 코딩 5문제
6-1) 단축평가 사용 while 문
6-2) 조건문 중복 (if 속에 if 그리고 ... else if)
6-3) 간접주소지정 이용
6-4) do while 문
6-5) bit masking 코드
7. 배열에 주어진 홀수의 합과 짝수의 합을 각각 다른 변수에 저장하는 어셈블리 코드
문제에서 가장 중요한 부분은 홀수와 짝수를 평가하는 부분이였음 (5점)
test array, 000000001로 끝 비트가 0인지 1인지를 구분해서 플래그값에 따라 분기하는 형식으로 했던것으로 기억남....
2014. 12. 17.
2014. 12. 15.
[MASM] 성적 산출 프로그램
1) C++ 구현 예
if (score < 0 | score > 100)
cout < "Erorr! The score is between 0 and 100“;
else if (score >= 90)
grade = ‘A’;
else if (score >= 80)
grade = ‘B’;
else if (score >= 70)
grade = ‘C’;
else if (score >= 60)
grade = ‘D’;
else
grade = ‘F’;
if (score < 0 | score > 100)
cout < "Erorr! The score is between 0 and 100“;
else if (score >= 90)
grade = ‘A’;
else if (score >= 80)
grade = ‘B’;
else if (score >= 70)
grade = ‘C’;
else if (score >= 60)
grade = ‘D’;
else
grade = ‘F’;
2) 어셈블리 코드
.data
str1 BYTE "Enter an integer score: ",0
str2 BYTE "Your grade is: ",0
str3 BYTE "Eror! The score is between 0 and 100", 0
.code
main PROC
call Clrscr
mov edx, OFFSET str1; input score from user
call WriteString
call ReadInt
call Crlf
cmp eax, 0 ; score < 0
jl Erorr ; 단축 평가 구현
cmp eax, 100 ; score > 100
jg Erorr
cmp eax, 90 ; score >= 90
jge Grade_A
cmp eax, 80 ; score >= 80
jge Grade_B
cmp eax, 70 ; score >= 70
jge Grade_C
cmp eax, 60 ; score >= 60
jge Grade_D
jmp Grade_F
str1 BYTE "Enter an integer score: ",0
str2 BYTE "Your grade is: ",0
str3 BYTE "Eror! The score is between 0 and 100", 0
.code
main PROC
call Clrscr
mov edx, OFFSET str1; input score from user
call WriteString
call ReadInt
call Crlf
cmp eax, 0 ; score < 0
jl Erorr ; 단축 평가 구현
cmp eax, 100 ; score > 100
jg Erorr
cmp eax, 90 ; score >= 90
jge Grade_A
cmp eax, 80 ; score >= 80
jge Grade_B
cmp eax, 70 ; score >= 70
jge Grade_C
cmp eax, 60 ; score >= 60
jge Grade_D
jmp Grade_F
Erorr:
mov edx, OFFSET str3
call WriteString
call crlf
exit
Grade_A:
mov al,'A'
jmp GRADE_PRN
Grade_B:
mov al,'B'
jmp GRADE_PRN
Grade_C:
mov al,'C'
jmp GRADE_PRN
Grade_D:
mov al,'D'
jmp GRADE_PRN
Grade_F:
mov al,'F'
GRADE_PRN:
mov edx,OFFSET str2
call WriteString
call WriteChar ; display grade leter in AL
call Crlf
exit
main ENDP
END main
[MASM] 배열의 최소값 구하기
Title 배열의 최소값 구하기
INCLUDE Irvine32.inc
IntegerCount = 20; array size
.data
intArray SDWORD 5 DUP(?)
min SDWORD ?
prompt1 BYTE "Enter an integer: ",0
prompt2 BYTE "Min : ",0
.code
main PROC
mov esi,OFFSET intArray
mov ecx, lengthof intArray
call PromptForIntegers ; 입력 다받음
mov esi, 0 ; index
mov eax, intArray[0] ; min
mov ecx, lengthof intArray-1 ; 4번 돌아야함
L1 :
add esi, type intarray ; 한칸증가 array[1] 부터 검사
cmp intarray[esi], eax ; 비교
jnl L2 ; 작지 안으면 반복
mov eax, intarray[esi] ; 작으면 교환
L2 : loop L1
mov edx,OFFSET prompt2
call WriteString ; display string
call WriteInt ; display EAX = min
call Crlf
exit
main ENDP
;-----------------------------------------------------
PromptForIntegers PROC
;
; 사용자로부터 배열에 정수를 입력 받는다.
; 매개변수 >> ESI : 더블형 정수 배열 주소, ECX : 배열의 크기
; Returns: the array contains the values
; entered by the user(반환값 >> 사용자에 의해 입력된 정수)
; Calls: ReadInt, WriteString(정수입력, 정수입력을 위한 안내문)
;-----------------------------------------------------
pushad ; save all registers
mov edx,OFFSET prompt1
L1:
call WriteString ; display string
call ReadInt ; read integer into EAX
mov [esi],eax ; store in array
add esi,4 ; next integer
loop L1
L2:
popad ; restore all registers
ret
PromptForIntegers ENDP
END main
INCLUDE Irvine32.inc
IntegerCount = 20; array size
.data
intArray SDWORD 5 DUP(?)
min SDWORD ?
prompt1 BYTE "Enter an integer: ",0
prompt2 BYTE "Min : ",0
.code
main PROC
mov esi,OFFSET intArray
mov ecx, lengthof intArray
call PromptForIntegers ; 입력 다받음
mov esi, 0 ; index
mov eax, intArray[0] ; min
mov ecx, lengthof intArray-1 ; 4번 돌아야함
L1 :
add esi, type intarray ; 한칸증가 array[1] 부터 검사
cmp intarray[esi], eax ; 비교
jnl L2 ; 작지 안으면 반복
mov eax, intarray[esi] ; 작으면 교환
L2 : loop L1
mov edx,OFFSET prompt2
call WriteString ; display string
call WriteInt ; display EAX = min
call Crlf
exit
main ENDP
;-----------------------------------------------------
PromptForIntegers PROC
;
; 사용자로부터 배열에 정수를 입력 받는다.
; 매개변수 >> ESI : 더블형 정수 배열 주소, ECX : 배열의 크기
; Returns: the array contains the values
; entered by the user(반환값 >> 사용자에 의해 입력된 정수)
; Calls: ReadInt, WriteString(정수입력, 정수입력을 위한 안내문)
;-----------------------------------------------------
pushad ; save all registers
mov edx,OFFSET prompt1
L1:
call WriteString ; display string
call ReadInt ; read integer into EAX
mov [esi],eax ; store in array
add esi,4 ; next integer
loop L1
L2:
popad ; restore all registers
ret
PromptForIntegers ENDP
END main
2014. 11. 11.
[MASM] 프로시저 디자인
기존의 코드를 재활용하기 위해 코드 작성전 프로시저를 디자인을 한다.
프로시저의 input과 output 그리고 call하는 프로시저 정도를 입력하고, 나중에 다시 봐도 알아보기 쉽도록 친절하게 작성한다.
11주차 프로시저 디자인 실습은 이전에 만들어논 배열의 합을 구하는 프로시저에서 GetIntegerCount 프로시저를 추가하여 입력받고자 하는 배열의 크기를 정한다.
Title 11주차 프로시저 디자인
INCLUDE Irvine32.inc
IntegerCount = 20; array size
.data
prompt1 BYTE "Enter a signed integer: ",0
prompt2 BYTE "The sum of the integers is: ",0
prompt3 BYTE " How many integers wil be added? : ",0
array DWORD IntegerCount DUP(?)
.code
;-----------------------------------------------------
main PROC
; Main program control procedure.(메인)
; Calls: Clrscr, PromptForIntegers,
; ArraySum, DisplaySum, GetIntegerCount
;-----------------------------------------------------
call Clrscr
mov esi,OFFSET array
call GetIntegerCount
mov ecx, eax ;; GetIntegerCount 에서 받은 eax를 루프카운터로 사용
call PromptForIntegers
call ArraySum
call DisplaySum
exit
main ENDP
;-----------------------------------------------------
GetIntegerCount PROC
; 기능 >> 입력 받을 정수의 갯수를 입력 받는 프로시저
; 매개변수 >> 없음
; 반환값 >> EAX : 실제 원소의 갯수
; Calls >> WriteString, ReadInt, Crlf
;-----------------------------------------------------
mov edx,OFFSET prompt3 ; display message
call WriteString
call ReadDec ; read Dec EAX
call Crlf
ret
GetIntegerCount ENDP
;-----------------------------------------------------
PromptForIntegers PROC
;
; 사용자로부터 배열에 정수를 입력 받는다.
; 매개변수 >> ESI : 더블형 정수 배열 주소, ECX : 배열의 크기
; 반환값 >> 사용자에 의해 입력된 정수)
; Calls: ReadInt, WriteString(정수입력, 정수입력을 위한 안내문)
;-----------------------------------------------------
pushad ; save all registers
mov edx,OFFSET prompt1
L1:
call WriteString ; display string
call ReadInt ; read integer into EAX
call Crlf ; go to next output line
mov [esi],eax ; store in array
add esi,4 ; next integer
loop L1
L2:
popad ; restore all registers
ret
PromptForIntegers ENDP
;-----------------------------------------------------
ArraySum PROC USES esi ecx
;
; 기능 >> 32비트 정수 배열의 합을 구한다
; 매개변수 >> ESI : 배열의 시작주소, ECX : 배열의 크기
; 반환값 >> EAX : 배열의 합
;-----------------------------------------------------
mov eax,0 ; set the sum to zero
L1:
add eax,[esi] ; add each integer to sum
add esi,4 ; point to next integer
loop L1 ; repeat for array size
L2:
ret
ArraySum ENDP
;------------------------------------------------
DisplaySum PROC
;
; 기능 >> 합을 출력한다.
; 매개변수 >> EAX : 합이 저장된 메모리
; 반환값 >> 없음
;------------------------------------------------
push edx
mov edx,OFFSET prompt2 ; display message
call WriteString
call WriteInt ; display EAX
call Crlf
pop edx
ret
DisplaySum ENDP
END main
프로시저의 input과 output 그리고 call하는 프로시저 정도를 입력하고, 나중에 다시 봐도 알아보기 쉽도록 친절하게 작성한다.
11주차 프로시저 디자인 실습은 이전에 만들어논 배열의 합을 구하는 프로시저에서 GetIntegerCount 프로시저를 추가하여 입력받고자 하는 배열의 크기를 정한다.
Title 11주차 프로시저 디자인
INCLUDE Irvine32.inc
IntegerCount = 20; array size
.data
prompt1 BYTE "Enter a signed integer: ",0
prompt2 BYTE "The sum of the integers is: ",0
prompt3 BYTE " How many integers wil be added? : ",0
array DWORD IntegerCount DUP(?)
.code
;-----------------------------------------------------
main PROC
; Main program control procedure.(메인)
; Calls: Clrscr, PromptForIntegers,
; ArraySum, DisplaySum, GetIntegerCount
;-----------------------------------------------------
call Clrscr
mov esi,OFFSET array
call GetIntegerCount
mov ecx, eax ;; GetIntegerCount 에서 받은 eax를 루프카운터로 사용
call PromptForIntegers
call ArraySum
call DisplaySum
exit
main ENDP
;-----------------------------------------------------
GetIntegerCount PROC
; 기능 >> 입력 받을 정수의 갯수를 입력 받는 프로시저
; 매개변수 >> 없음
; 반환값 >> EAX : 실제 원소의 갯수
; Calls >> WriteString, ReadInt, Crlf
;-----------------------------------------------------
mov edx,OFFSET prompt3 ; display message
call WriteString
call ReadDec ; read Dec EAX
call Crlf
ret
GetIntegerCount ENDP
;-----------------------------------------------------
PromptForIntegers PROC
;
; 사용자로부터 배열에 정수를 입력 받는다.
; 매개변수 >> ESI : 더블형 정수 배열 주소, ECX : 배열의 크기
; 반환값 >> 사용자에 의해 입력된 정수)
; Calls: ReadInt, WriteString(정수입력, 정수입력을 위한 안내문)
;-----------------------------------------------------
pushad ; save all registers
mov edx,OFFSET prompt1
L1:
call WriteString ; display string
call ReadInt ; read integer into EAX
call Crlf ; go to next output line
mov [esi],eax ; store in array
add esi,4 ; next integer
loop L1
L2:
popad ; restore all registers
ret
PromptForIntegers ENDP
;-----------------------------------------------------
ArraySum PROC USES esi ecx
;
; 기능 >> 32비트 정수 배열의 합을 구한다
; 매개변수 >> ESI : 배열의 시작주소, ECX : 배열의 크기
; 반환값 >> EAX : 배열의 합
;-----------------------------------------------------
mov eax,0 ; set the sum to zero
L1:
add eax,[esi] ; add each integer to sum
add esi,4 ; point to next integer
loop L1 ; repeat for array size
L2:
ret
ArraySum ENDP
;------------------------------------------------
DisplaySum PROC
;
; 기능 >> 합을 출력한다.
; 매개변수 >> EAX : 합이 저장된 메모리
; 반환값 >> 없음
;------------------------------------------------
push edx
mov edx,OFFSET prompt2 ; display message
call WriteString
call WriteInt ; display EAX
call Crlf
pop edx
ret
DisplaySum ENDP
END main
[MASM] 스택의 활용
Title 스택의 활용
INCLUDE Irvine32.inc
.data
inputStr BYTE "Enter Your Name : ",0
resultStr BYTE "Reverse String : ",0
aName BYTE 80 DUP(?)
byteCount DWORD ?
nameSize = ($ - aName) - 1
.code
main PROC
;이름 입력
mov edx,OFFSET inputStr
call WriteString
;이름 입력
mov edx, offset aName
mov ecx, sizeof aName
call ReadString
mov byteCount, eax
call Crlf
; 스택에 푸시
mov ecx,nameSize
mov esi,0
L1:
movzx eax, aName[esi]; get character
push eax; push on stack
inc esi
Loop L1
; Pop the name from the stack, in reverse,
; and store in the aName array.
mov ecx,nameSize
mov esi,0
L2:
pop eax; get character
mov aName[esi], al; store in string
inc esi
Loop L2
mov edx,OFFSET resultStr
call WriteString
mov edx, offset aName
call WriteString
mov edx, OFFSET aName
call WriteString
mov edx, offset aName
mov ecx, LENGTHOF aName
call ReadString
mov byteCount, eax
call Crlf
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
inputStr BYTE "Enter Your Name : ",0
resultStr BYTE "Reverse String : ",0
aName BYTE 80 DUP(?)
byteCount DWORD ?
nameSize = ($ - aName) - 1
.code
main PROC
;이름 입력
mov edx,OFFSET inputStr
call WriteString
;이름 입력
mov edx, offset aName
mov ecx, sizeof aName
call ReadString
mov byteCount, eax
call Crlf
; 스택에 푸시
mov ecx,nameSize
mov esi,0
L1:
movzx eax, aName[esi]; get character
push eax; push on stack
inc esi
Loop L1
; Pop the name from the stack, in reverse,
; and store in the aName array.
mov ecx,nameSize
mov esi,0
L2:
pop eax; get character
mov aName[esi], al; store in string
inc esi
Loop L2
mov edx,OFFSET resultStr
call WriteString
mov edx, offset aName
call WriteString
mov edx, OFFSET aName
call WriteString
mov edx, offset aName
mov ecx, LENGTHOF aName
call ReadString
mov byteCount, eax
call Crlf
exit
main ENDP
END main
[MASM] 스택 사용2( 정수)
TITLE 11월 3일 실습 스택사용
INCLUDE Irvine32.inc
.data
prompt3 BYTE "입력할 정수의 갯수 : ",0
prompt4 BYTE "정수 입력 : ",0
prompt5 BYTE "Reverse Integer Array : ",0
input DWORD ? ;입력받을 정수갯수
intArray SDWORD arraySize DUP(?);정수를 저장할 정수 배열
mov edx, OFFSET prompt3
call WriteString
call ReadDec ;입력 갯수라서 부호없는 정수사용
mov input, eax
arraySize = input ;등호디릭티브로 배열크기 조정
call Crlf
;정수를 배열에 입력
mov ecx, input
mov esi, 0
L3:
mov edx, OFFSET prompt4
call WriteString
call ReadInt
mov intArray[esi], eax
add esi, TYPE intArray
call Crlf
Loop L3
;배열의 내용을 스택에 푸시
mov ecx, input
mov esi, 0
L4:
mov eax, intArray[esi]
push eax
add esi, TYPE intArray
Loop L4
;스택을 팝하여 역순으로 출력
;원 정수 배열은 변경되지 않음
mov ecx, input
mov edx, OFFSET prompt5
call WriteString
L5:
pop eax
call WriteInt
Loop L5
call Crlf
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
prompt3 BYTE "입력할 정수의 갯수 : ",0
prompt4 BYTE "정수 입력 : ",0
prompt5 BYTE "Reverse Integer Array : ",0
input DWORD ? ;입력받을 정수갯수
intArray SDWORD arraySize DUP(?);정수를 저장할 정수 배열
mov edx, OFFSET prompt3
call WriteString
call ReadDec ;입력 갯수라서 부호없는 정수사용
mov input, eax
arraySize = input ;등호디릭티브로 배열크기 조정
call Crlf
;정수를 배열에 입력
mov ecx, input
mov esi, 0
L3:
mov edx, OFFSET prompt4
call WriteString
call ReadInt
mov intArray[esi], eax
add esi, TYPE intArray
call Crlf
Loop L3
;배열의 내용을 스택에 푸시
mov ecx, input
mov esi, 0
L4:
mov eax, intArray[esi]
push eax
add esi, TYPE intArray
Loop L4
;스택을 팝하여 역순으로 출력
;원 정수 배열은 변경되지 않음
mov ecx, input
mov edx, OFFSET prompt5
call WriteString
L5:
pop eax
call WriteInt
Loop L5
call Crlf
exit
main ENDP
END main
[MASM] 스택 사용1 (문자열)
TITLE 11월 3일 실습 스택사용
INCLUDE Irvine32.inc
arraySize = 10
.data
prompt1 BYTE "Enter Your Name : ",0
prompt2 BYTE "Reverse String : ",0
inputName BYTE 80 DUP(0) ;이름
byteCount DWORD ? ;입력받은 문자수
.code
main PROC
mov edx, OFFSET prompt1
call WriteString
;이름 입력
mov edx, OFFSET inputName ;ReadString의 오프셋
mov ecx, SIZEOF inputName ;ReadString의 최대 문자수에 1을 더한값
call ReadString
mov byteCount, eax ;문자의 수
call Crlf
;스택에 push
mov ecx, byteCount ;입력된 문자수 만큼만 푸시
mov esi, 0
L1:
movzx eax, inputName[esi]
push eax
inc esi
Loop L1
;스택의 내용을 pop
mov ecx, byteCount ;입력된 문자수 만큼만 푸시
mov esi, 0
L2:
pop eax
mov inputName[esi], al
inc esi
Loop L2
;역으로 출력
mov edx, OFFSET prompt2
call WriteString
mov edx, OFFSET inputName
call WriteString
call Crlf
call Crlf
exit
main ENDP
END main
INCLUDE Irvine32.inc
arraySize = 10
.data
prompt1 BYTE "Enter Your Name : ",0
prompt2 BYTE "Reverse String : ",0
inputName BYTE 80 DUP(0) ;이름
byteCount DWORD ? ;입력받은 문자수
.code
main PROC
mov edx, OFFSET prompt1
call WriteString
;이름 입력
mov edx, OFFSET inputName ;ReadString의 오프셋
mov ecx, SIZEOF inputName ;ReadString의 최대 문자수에 1을 더한값
call ReadString
mov byteCount, eax ;문자의 수
call Crlf
;스택에 push
mov ecx, byteCount ;입력된 문자수 만큼만 푸시
mov esi, 0
L1:
movzx eax, inputName[esi]
push eax
inc esi
Loop L1
;스택의 내용을 pop
mov ecx, byteCount ;입력된 문자수 만큼만 푸시
mov esi, 0
L2:
pop eax
mov inputName[esi], al
inc esi
Loop L2
;역으로 출력
mov edx, OFFSET prompt2
call WriteString
mov edx, OFFSET inputName
call WriteString
call Crlf
call Crlf
exit
main ENDP
END main
2014. 10. 30.
[MASM] 배열의 역순
TITLE 배열의 역순 (인덱스 주소지정 사용)
INCLUDE Irvine32.inc
.data
arrayD DWORD 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
before BYTE "변경 전 arrayD",0
after BYTE "변경 후 arrayD",0
.code
main PROC
;맨 처음 배열 보이기
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET before
call WriteString
call DumpMem
call Crlf
mov esi, 0 ;배열의 가장 앞 주소
mov edi, OFFSET (SIZEOF arrayD - TYPE arrayD) ;배열의 가장 뒤 4바이트를 가리키는 가장 처음 주소
mov ecx, (LENGTHOF arrayD) / 2 ;루프 카운터
L1:
mov eax, [arrayD + esi] ;esi가 가리키는 곳을 eax에 복사
xchg eax, [arrayD + edi] ;eax와 edi가 가리키는 곳을 교환
mov [arrayD + esi], eax ;esi가 가리키는 곳에 eax로 복사
add esi, TYPE arrayD ;esi를 4만큼 올림
sub edi, TYPE arrayD ;edi를 4만큼 내림
loop L1
;변경 후 배열 보이기
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET after
call WriteString
call DumpMem
call Crlf
exit
main ENDP
END main
------------------------------------------------------------------------------------------------------
TITLE 배열의 역순 (간접피연산자 사용)
INCLUDE Irvine32.inc
.data
arrayD DWORD 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
before BYTE "변경 전 arrayD",0
after BYTE "변경 후 arrayD",0
.code
main PROC
;변경 전 배열 출력
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET before
call WriteString
call DumpMem
call Crlf
mov esi, OFFSET arrayD ;배열의 가장 앞 주소
mov edi, OFFSET arrayD + SIZEOF arrayD - TYPE arrayD;배열의 가장 뒤 4바이트를 가리키는 가장 처음 주소
mov ecx, (LENGTHOF arrayD) / 2 ;루프 카운터
L1:
mov eax, [esi] ;esi가 가리키는 곳의 값을 eax에 복사
xchg eax, [edi] ;eax와 edi가 가리키는 곳의 값을 교환
mov [esi], eax ;esi가 가리키는 곳의 값을 eax로 복사
add esi, TYPE arrayD ;esi를 offset을 4만큼 올림
sub edi, TYPE arrayD ;edi를 offset을 4만큼 내림
loop L1
;변경 후 배열 출력
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET after
call WriteString
call DumpMem
call Crlf
exit
main ENDP
END main
INCLUDE Irvine32.inc
.data
arrayD DWORD 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
before BYTE "변경 전 arrayD",0
after BYTE "변경 후 arrayD",0
.code
main PROC
;맨 처음 배열 보이기
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET before
call WriteString
call DumpMem
call Crlf
mov esi, 0 ;배열의 가장 앞 주소
mov edi, OFFSET (SIZEOF arrayD - TYPE arrayD) ;배열의 가장 뒤 4바이트를 가리키는 가장 처음 주소
mov ecx, (LENGTHOF arrayD) / 2 ;루프 카운터
L1:
mov eax, [arrayD + esi] ;esi가 가리키는 곳을 eax에 복사
xchg eax, [arrayD + edi] ;eax와 edi가 가리키는 곳을 교환
mov [arrayD + esi], eax ;esi가 가리키는 곳에 eax로 복사
add esi, TYPE arrayD ;esi를 4만큼 올림
sub edi, TYPE arrayD ;edi를 4만큼 내림
loop L1
;변경 후 배열 보이기
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET after
call WriteString
call DumpMem
call Crlf
exit
main ENDP
END main
------------------------------------------------------------------------------------------------------
TITLE 배열의 역순 (간접피연산자 사용)
INCLUDE Irvine32.inc
.data
arrayD DWORD 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
before BYTE "변경 전 arrayD",0
after BYTE "변경 후 arrayD",0
.code
main PROC
;변경 전 배열 출력
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET before
call WriteString
call DumpMem
call Crlf
mov esi, OFFSET arrayD ;배열의 가장 앞 주소
mov edi, OFFSET arrayD + SIZEOF arrayD - TYPE arrayD;배열의 가장 뒤 4바이트를 가리키는 가장 처음 주소
mov ecx, (LENGTHOF arrayD) / 2 ;루프 카운터
L1:
mov eax, [esi] ;esi가 가리키는 곳의 값을 eax에 복사
xchg eax, [edi] ;eax와 edi가 가리키는 곳의 값을 교환
mov [esi], eax ;esi가 가리키는 곳의 값을 eax로 복사
add esi, TYPE arrayD ;esi를 offset을 4만큼 올림
sub edi, TYPE arrayD ;edi를 offset을 4만큼 내림
loop L1
;변경 후 배열 출력
mov esi, OFFSET arrayD
mov ecx, LENGTHOF arrayD
mov ebx, TYPE arrayD
mov edx, OFFSET after
call WriteString
call DumpMem
call Crlf
exit
main ENDP
END main
2014. 10. 29.
[MASM] 문자열의 역방향 복사
TITLE 문자열 역순구하기
COMMENT ?
인덱스 피연산자를 활용
?
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi, LENGTHOF source -2
mov edi, 0
mov ecx, SIZEOF source
L1:
mov al,source[esi]
mov target[edi],al
dec esi
inc edi
loop L1
mov esi, OFFSET target-1
mov ebx, 1
mov ecx, SIZEOF target
call DumpMem
mov edx, OFFSET target
call WriteString
call Crlf
exit
main ENDP
END main
-----------------------------------------------------------
TITLE 문자열 역순구하기
COMMENT ?
직접 피연산자를 활용
?
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi, 0
mov ecx, LENGTHOF source
dec ecx
L1:
mov al,source[ecx-1]
mov target[esi],al
inc esi
loop L1
mov esi,OFFSET target
mov ebx,1
mov ecx,SIZEOF target-1
mov edx,OFFSET target
call DumpMem
call writeString
call Crlf
exit
main ENDP
END main
COMMENT ?
인덱스 피연산자를 활용
?
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi, LENGTHOF source -2
mov edi, 0
mov ecx, SIZEOF source
L1:
mov al,source[esi]
mov target[edi],al
dec esi
inc edi
loop L1
mov esi, OFFSET target-1
mov ebx, 1
mov ecx, SIZEOF target
call DumpMem
mov edx, OFFSET target
call WriteString
call Crlf
exit
main ENDP
END main
-----------------------------------------------------------
TITLE 문자열 역순구하기
COMMENT ?
직접 피연산자를 활용
?
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP(0)
.code
main PROC
mov esi, 0
mov ecx, LENGTHOF source
dec ecx
L1:
mov al,source[ecx-1]
mov target[esi],al
inc esi
loop L1
mov esi,OFFSET target
mov ebx,1
mov ecx,SIZEOF target-1
mov edx,OFFSET target
call DumpMem
call writeString
call Crlf
exit
main ENDP
END main
피드 구독하기:
글 (Atom)