06 环形矩阵

来源:互联网 发布:英语教科书辅导软件 编辑:程序博客网 时间:2024/05/18 13:11

前言
这个概念貌似还不好说.., 搜了一下”环形矩阵”, 但是似乎没有这个概念
声明 : 这种矩阵可能不叫环形矩阵, 只不过是我将其称呼为环形矩阵
举个例子吧 : 这里写图片描述

参考代码

assume cs:code, ds:data, ss:stackdata segment         notice db 'please input a number [press "q" to exit] : $ ' ;    notice information        matrix db 100 dup (0)       ; save the data of matrix         buffer db 3                 ; user could input just two byte               db 0               db 3 dup (0)        input db 0                  ; the width of matrix           matrixWidth dw 10           ; the width of matrix        resultWidth dw 42           ; the width of result matrix[for show to user]        result db 420 dup(0)        ; the result matrix[for show to user]        tmp dw 2 dup(0)             ; the temp memory to save the offset while change number to ascii        br db 0aH, 0dH, '$'data endsstack segment        db 200 dup (0)stack endscode segmentstart:        mov ax, data        mov ds, ax        mov ax, stack        mov ss, ax        ; mov ah, 1        ; int 21H        lea dx, notice      ; output notice information        call outputInfo        lea dx, buffer      ; let user input        mov ah, 10        int 21H    codeLoop1:          call cls        lea dx, br        call outputInfo     ; output a break line        mov cl, buffer[1]        mov ch, 0        cmp buffer[2], 'q'        jz codeEnd        mov di, cx          ; 'mov di, cx+1' is error        inc di        mov al, 1        mov dx, 0        call getNumValue    ; get the number value user inputed        mov input, dl       ; save it into input memory        mov cl, input        mov ch, 0        call calcMatrixData     ; check the cx register!!!!!!!!!!!! , get the matrix's data         call calcResultData     ; get the result information        call showResult         ; show result information        lea dx, notice      ; output notice information        call outputInfo        lea dx, buffer      ; let user input        mov ah, 10        int 21H    loop codeLoop1        codeEnd:        mov ax, 4c00H           ; return to cmd        int 21HoutputInfo proc near            ; the process to output something, the information save in begin with 'dx', end with '$'        push ax        mov ah, 9        int 21H        pop ax        retoutputInfo endp showResult proc near            ; the process to show result information    push di    push dx    mov di, 0    showResultLoop1:        lea dx, result[di]        call outputInfo         ; output a row        add di, 42    loop showResultLoop1        pop dx    pop di    retshowResult endp     calcResultData proc near        ; the process to get the result data[number value -> ascii]    push si    push di    push cx     ; the number value user inputed    push ax     ; assist register    push bx    mov si, 0    mov di, 0    mov ax, cx    calcResultDataLoop1:    push cx             mov cx, ax        calcResultDataLoop2:            call getMatrixData              ; get data from matrix by 'si' and 'di'            push di            call getResultIndexBySiAndDi    ; get result index by 'si' and 'di'            mov tmp, di            inc di            mov tmp[2], di                      call calcHalfWord               ; put the matrix data input result matrix            inc di                  ; add space, for view            mov result[di], 32                  inc di            mov result[di], 32            pop di            inc di        loop calcResultDataLoop2        mov di, tmp[2]        add di, 2        mov result[di], 0aH         ; append the break line and '$' over symbol        inc di        mov result[di], 0dH        inc di        mov result[di], '$'        inc si        mov di, 0    pop cx    loop calcResultDataLoop1    ; mov cx, ax    ; mov di, 40    ; calcResultDataLoop3:    ; inc di    ; mov result[di], 0aH    ; inc di    ; mov result[di], 0dH    ; add di, 42    ; loop calcResultDataLoop3    pop bx    pop ax    pop cx    pop di    pop si    retcalcResultData endp         getResultIndexBySiAndDi proc near           ; get result matrix index by 'si' and 'di'        push ax        push bx        mov ax, si        mov bx, ds:[resultWidth]        mul bl                  ; the result matrix width is 42        mov bx, ax        mov ax, 4        mul di        add ax, bx        mov di, ax        pop bx        pop ax        retgetResultIndexBySiAndDi endp    getMatrixData proc near         ; the function to get data of data matrix by 'si' and 'di', the data saved in 'bx'        push di        call getMatrixIndexBySiAndDi        mov bl, matrix[di]        mov bh, 0        pop di        retgetMatrixData endp  calcHalfByte    proc near   ; calc the bl [----just judge lower four bit----]' s corresponding ascii code, save in bl        ; push bx           ; bl record the data[need to be transfer]        push cx         ; record the offset to save in exp data segment        cmp bl, 9        ja abc                                  ; bl not bigger than 9        ; mov [exp+cx], bl+48       ; 1 ~ 8        add bl, 48        jmp calcHalfByteEnd        abc:            ; bl bigger than 9        ; mov [exp+cx], bl+87       ; A ~ F            add bl, 87    calcHalfByteEnd:                pop cx        retcalcHalfByte endp   calcHalfWord proc               ; to calcute two hexadecimal number, number saved in bl, need to push two offset relative exp flag to save data    push bx    push di    push dx    push cx    ; add sp, 4                 ; mov stack pointer to save data offset, why there need just add 2 bit ? it just push ip in stack ?    mov dx, bx    mov cl, 4                   ; calc the upper 4 bit     shr bl, cl    call calcHalfByte    ; pop di    mov di, ds:[tmp]            ; the segment of tmp must match the left value[cx], use dw to define data    mov ds:[result+di], bl    mov bx, dx                  ; calc the lower 5 bit    and bl, 15    call calcHalfByte    ; pop di    mov di, ds:[tmp+2]    mov ds:[result+di], bl    ; sub sp, 6                 ; mov stack pointer to the real stack top                                ; why the value in stack changed ???        --2014.07.25    pop cx    pop dx    pop di    pop bx    retcalcHalfWord endp calcOneWord proc near           ; to calcute two hexadecimal number, number saved in 'bx', 'di' saved the memory offset,    push dx    push di    mov dx, bx    mov cl, 8                   ; calc the upper 8 bit of bx    shr bx, cl    inc di    mov ds:[tmp], di    inc di    mov ds:[tmp+2], di    call calcHalfWord    mov bx, dx                  ; calc the lower 8 bit of bx    mov bh, 0    inc di    mov ds:[tmp], di    inc di    mov ds:[tmp+2], di    call calcHalfWord    pop di    pop dx    retcalcOneWord endpcalcMatrixData proc near            ; put snake data matrix's data into matrix memory        push si        push di        push dx         ; the index 1, 2, 3, ...        push cx         ; the length user inputed        push ax         ; the assist register        mov dx, 0        mov si, 0        mov di, 0        dec cx          ; the number value user inputed        cmp cx, 0       ; if user inputed is 1, jump to 'haveOneData'        jz haveOneData        calcMatrixDataLoop1:            ; cmp cx, 0            ; jz haveOneData            cmp cx, matrixWidth             ; set cx bigger than f0 is overflow            ja calcMatrixDataEnd                ; if cx decrease to lower than 0,all data put completed , get to end             mov ax, cx            plusDi:                call putData                                inc di                inc dx            loop plusDi            mov cx, ax            plusSi:                call putData                inc si                inc dx            loop plusSi            mov cx, ax            subDi:                call putData                                dec di                inc dx            loop subDi            mov cx, ax            subSi:                call putData                                dec si                inc dx            loop subSi            inc si              ; increase si, di to makes them at next position            inc di            mov cx, ax            dec cx              ; include loop cx will be decrease 2        loop calcMatrixDataLoop1        ; get there if cx is 0        haveOneData:            ; it will get there whill cx is 0            call putData        calcMatrixDataEnd:        pop ax        pop cx        pop dx        pop di        pop si        retcalcMatrixData endp     putData proc near               ; put data into memory,         push di        call getMatrixIndexBySiAndDi        mov matrix[di], dl      ; put the dx into memory        pop di        retputData endp getMatrixIndexBySiAndDi proc near   ; get index of data matrix by 'si' and 'di'        push ax        push bx        mov ax, si        mov bx, ds:[matrixWidth]        mul bl                  ; the matrix width is 10        add ax, di              ; ax = 10 * si + di        mov di, ax        pop bx        pop ax        retgetMatrixIndexBySiAndDi endp        getNumValue proc near               ; get operand NumberValue by the number string in memory 1 2 3 -> 123        push di                     ; 'dx' save the ultamate result, 'di' save the last offset of input, 'al' is 1 base number, cx is the count of input        push ax        push cx        getOperandValueLoop1:            mov ah, buffer[di]            call getNumValueByAscii            push ax            mul ah            add dx, ax            pop ax            mov ah, 10            mul ah              dec di        loop getOperandValueLoop1        pop cx        pop ax        pop di        retgetNumValue endpgetNumValueByAscii proc near                    ; get NumberValue by ascii        cmp ah, 48        jae numFlag1        jmp getNumValueEnd        numFlag1:        cmp ah, 57        jbe numFlag2        jmp alphaFlag1        numFlag2:            sub ah, 48        jmp getNumValueEnd        alphaFlag1:        cmp ah, 97        jae alphaFlag2        jmp getNumValueEnd        alphaFlag2:        cmp ah, 102        ja getNumValueEnd        sub aH, 87        getNumValueEnd:        retgetNumValueByAscii endp     cls proc near           ; to clear the screen      push ax      push bx      push cx      push dx      mov  ah,6      mov al,0      mov ch,0      mov cl,0      mov dh,20      mov dl,79      mov bh,7      int 10h      pop dx      pop cx      pop bx      pop ax      retcls endpcode endsend start        ; made by 970655147     --2014.07.30

效果截图 : 这里写图片描述

0 0