24h購物| | PChome| 登入
2013-03-24 20:02:10| 人氣6,958| 回應2 | 上一篇 | 下一篇

[組合語言][作業] convert pattern (binary to string)

推薦 0 收藏 0 轉貼0 訂閱站台

作業內容:

—(10) Design any 8x8 bit icon with 0,1-pattern, and saved in BStrs (BStr0~BStr7).
—(20) A procedure: Convert 0 to blank and 1 to x for each bit in BStrs to the corresponding char in CStrs.
寫一個procedure,它的功能是將BStrs中的bit抽出並轉換為指定的字元(0轉換為空白,1轉為x),放到CStrs變數。
—(10) A do loop which calls the converting procedure 8 times to convert the 8 bit strings to the 8 char string.
程式碼中需使用到Loop指令,並呼叫你所寫的procedure


作業規定:
宣告變數 Declare

BStr0 db ?

BStr1 db ?

BStr2 db ?

BStr3 db ?

BStr4 db ?

BStr5 db ?

BStr6 db ?

  BStr7 db ?

CStr0 db 8 dup (?)

CStr1 db 8 dup (?)

CStr2 db 8 dup (?)

CStr3 db 8 dup (?)

CStr4 db 8 dup (?)

CStr5 db 8 dup (?)

CStr6 db 8 dup (?)

CStr7 db 8 dup (?)

紅色部分不能變動










心得

這次作業多了一些 call function 的指令,而同時練習到自己寫函數去呼叫,由於暫存器就那麼幾個,
因此必須叫函數前,把參數傳入到特定的暫存器,假如被呼叫的函數內部會修改某些暫存器,
為了不影響原本呼叫的函數繼續執行,要先將這些變數先丟入堆疊中,而在被呼叫函數結束後被複製回來。
切記在回傳一定要打上 ret,不然會發生無法預期的錯誤 ASM 並沒有像 MIPS 指令複雜,
不用自己操作
eps 的值。
由於不可能對於每個變數都呼叫一次轉換,程式可能簡單好懂,但過於累贅,因此採用 BYTE address 
方式去讀取,只抓第一個變數的位址,然後依序累加,同理輸出的位址。
 
程式碼說明
程式分成三個部分去處理
第一部分:
單處理一個8 bits 的數字轉換成 ASCII,從最高位依序納入低位元位址。
第二部分:
給定一個 address,將接續的 64 bytes,以8*8 的矩陣方式印出,以 ASCII 顯示。
第三部分:
分別依序呼叫八個變數,將這八個變數的值存入給定的位址。


程式碼:


TITLE

INCLUDE Irvine32.inc
.data
BStr0 db 01111110b
BStr1 db 10000001b
BStr2 db 10100101b
BStr3 db 10000001b
BStr4 db 10100101b
BStr5 db 10111101b
BStr6 db 10000001b
BStr7 db 01111110b
CStr0 db 8 dup(?)
CStr1 db 8 dup(?)
CStr2 db 8 dup(?)
CStr3 db 8 dup(?)
CStr4 db 8 dup(?)
CStr5 db 8 dup(?)
CStr6 db 8 dup(?)
CStr7 db 8 dup(?)
.code
convertPattern PROC
; al = binary integer, esi pointer to buffer
    push ecx
    push esi
   
    mov ecx, 8             ; number of bits in AL
L1: shl al, 1              ; shift high bit into carry flag
    mov BYTE PTR[esi], ' ' ; choose ' ' as default output
    jnc L2                 ; if no Carry, jump L2
    mov BYTE PTR[esi], '*' ; else move '*' to buffer
L2: inc esi                ; next buffer position
    loop L1                ; shift another bit to left
   
    pop esi
    pop ecx
    ret
convertPattern ENDP

printResult PROC
; esi pointer to buffer, print[8][8]
    push ecx
    push esi
   
    mov ecx, 8            ; number of rows
L1: push ecx
    mov ecx, 8            ; number of column
L2: mov al, BYTE PTR[esi] ; move buffer to AL
    inc esi               ; next buffer position
    call WriteChar        ; print AL(ASCII)
    loop L2               ; next column
   
    call Crlf             ; print '\n'
    pop ecx
    loop L1               ; next row
   
    pop esi
    pop ecx
    ret
printResult ENDP

start PROC
    mov ebx, 0            ; BStr[ebx]
    mov ecx, 8            ; run BStr[0-7]
    mov esi, OFFSET CStr0 ; move buffer position to esi
L1: mov al, BStr0[ebx]    ; move byte BStr[ebx] to AL
    call convertPattern   ; convert bit pattern
    add esi, SIZEOF CStr0 ; next buffer position
    add ebx, SIZEOF BStr0 ; next convert byte
    loop L1
    mov esi, OFFSET CStr0 ; output
    call printResult
    call WaitMsg
    invoke ExitProcess, 0
start ENDP;
end start;

台長: Morris
人氣(6,958) | 回應(2)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: [學習]組合語言 |
此分類下一篇:[MIPS] 泡泡排序 Bubble Sort
此分類上一篇:[MIPS] 輸入、輸出數字範例

Morris
此代碼在之後的作業 3 中會做修改,以及使用 MACRO 的方式撰寫。
2013-04-29 21:17:32
(悄悄話)
2017-11-03 00:42:34
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文