Programming

Just another WordPress.com weblog

ข้อสอบ CT215 ปี 2551 เขียน code assembly ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 8, 2008

เป็นส่วนของการเขียน program ยาว ซึ่งเป็นตัวอย่างหนึ่งเท่านั้น อาจารย์สามารถออกโจทย์ได้เยอะมากครับ สำหรับโจทย์ข้อนี้ผมเอา code ของอาจารย์ที่ให้ไว้มาปรับปรุงครับ

โจทย์ ให้พิมพ์ตัวอักขระใดๆ แล้วเมื่อกด Enter ให้เขียน String นั้นเริ่มจากขวาไปซ้าย (Reverse String)

Program Reverse String
Insert Sample String: abcdef
Reverse String: fedcba
Run Again (y/n)

————
TITLE Program Template (MyRevStr.asm)

; This program reverses a string.
; Last update: 1/28/02

INCLUDE Irvine32.inc
Include Macros.inc

.data

buffer BYTE 128 DUP(0)
byteCount DWORD ?

.code
main PROC

Start:

call Clrscr

mGotoxy 20,10
mWrite <”Program Reverse String”,0dh,0ah>

mGotoxy 20,11
mWrite “Insert Sample String: “

mov edx, OFFSET buffer
mov ecx, SIZEOF buffer

call ReadString

mov byteCount, eax

cmp eax, 0
je DISPLAY

; Push the name on the stack.
mov ecx,byteCount
mov esi,0

L1:

movzx eax,buffer[esi] ; get character
push eax ; push on stack
inc esi
Loop L1

; Pop the name from the stack, in reverse,
; and store in the buffer array.
mov ecx,byteCount
mov esi,0

L2:

pop eax ; get character
mov buffer[esi],al ; store in string
inc esi
Loop L2

DISPLAY:

; Display the name.
mGotoxy 20,12
mWrite “Reverse String: “

mov edx,OFFSET buffer
call Writestring
call Crlf

mGotoxy 20,13
mWrite “Run Again (y/n) “
call ReadChar

cmp al,’y’
je Start

exit
main ENDP
END main

ใส่ความเห็น

XHTML: คุณสามารถใช้แท็กเหล่านี้ได้: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>