程式設計雜筆

[程設雜筆] 初探MASM組合語言+課本習題練習

這學期選了一門組合語言,話不多說,直接上程式碼


Converting from big endian to little endian


TITLE ch4-1

include Irvine32.inc
include Macros.inc

.data
bigE BYTE 12h, 34h, 56h, 78h
littleE DWORD ?
little BYTE 0,0,0,0
.code
main PROC
mov ebx,0
mov ecx, 3
mov esi, offset bigE   ;save bigE address
mov edi, offset little ;save little address

L1:
mov al, [esi+ebx]
mov [edi+ecx], al
dec ecx
inc ebx
cmp ebx, 4
jne L1

mov eax, DWORD PTR little ;mov little array into littleE
mov littleE,eax
mov eax,littleE
call WriteHex ;print
call ReadInt
exit
main ENDP
end main</pre>
<pre>

output:
12345678

copy a string in reverse order

TITLE ch4-1

include Irvine32.inc
include Macros.inc

.data
 source BYTE "1234567890",0
 target BYTE SIZEOF source DUP(' ')

.code
main PROC
 mov esi,OFFSET source ;esi ptr of source
 mov edi,esi           ;edi initial place-1
 mov ebp,OFFSET target ;ebp ptr of target
 sub edi,1
 add esi,SIZEOF source
 sub esi,2
L1:
 mov al,[esi]
 call WriteChar
 mov [ebp],al
 dec esi               ;esi 遞減
 inc ebp               ;ebp 遞增
 call DumpRegs
 cmp esi,edi
 jne L1

 mov edx,OFFSET source   ;列印
 call WriteString
 mov al,0Ah
 call WriteChar
 mov edx,OFFSET target
 call WriteString
 call ReadInt
exit
main ENDP
end main

output:
1234567890
0987654321

好累,第一次寫,寫三題寫了三個小時= =
寫C大概10分鐘就寫完了QAQ

參考資料:
http://blog.xuite.net/asd.wang/alog/269353-%5BMasm%5D+Assembly+%E7%AD%86%E8%A8%98+-+Ch5+%E7%A8%8B%E5%BA%8F

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s