Difference between revisions of "CSC231 Homework 6 Solution"
(New page: <code><pre> ;;; hw6.asm ;;; Amy Gray ;;; This program reads a bmp file into memory and decodes a secret ;;; message hidden in the pixels via steganography ;;; %include "mytools2.inc" ; f...) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | < | + | <onlydft> |
+ | <source lang="asm"> | ||
;;; hw6.asm | ;;; hw6.asm | ||
− | ;;; | + | ;;; Ellysha Raelen Recto |
− | ;;; This program reads a bmp file | + | ;;; |
− | ;;; | + | ;;; This program written from the skeleton program, makegreyBmp.asm, written |
+ | ;;; by D. Thiebaut. This program reads a bmp file and reads the last bit of | ||
+ | ;;; each byte. The bytes read contains a secret msg which is then displayed. | ||
;;; | ;;; | ||
%include "mytools2.inc" ; for basic printing to the screen | %include "mytools2.inc" ; for basic printing to the screen | ||
%include "fileio2.inc" ; for basic file operations | %include "fileio2.inc" ; for basic file operations | ||
− | + | ||
%assign SYS_EXIT 1 | %assign SYS_EXIT 1 | ||
%assign MAXPIXEL 5000000 | %assign MAXPIXEL 5000000 | ||
+ | |||
+ | %assign SYS_WRITE 4 | ||
+ | %assign STDOUT 1 | ||
%assign RED 2 | %assign RED 2 | ||
Line 23: | Line 29: | ||
errorMsg db "Error accessing file!",0x0A | errorMsg db "Error accessing file!",0x0A | ||
errorLen equ $-errorMsg | errorLen equ $-errorMsg | ||
− | + | msg db 0 | |
section .bss | section .bss | ||
− | filename resb 255 | + | filename resb 255 ; reserve 255 chars for filename |
handle resd 1 ; file descriptor | handle resd 1 ; file descriptor | ||
noRead resd 1 ; contains number of bytes read | noRead resd 1 ; contains number of bytes read | ||
lWidth resd 1 ; the line width rounded to the nearest | lWidth resd 1 ; the line width rounded to the nearest | ||
− | ; dword boundary lWidth=(((width-1)/4)+1)*4 | + | ; dword boundary lWidth=(((width-1)/4)+1)*4 |
;;; ---------------------------------------------------------- | ;;; ---------------------------------------------------------- | ||
Line 83: | Line 89: | ||
cmp al,0 | cmp al,0 | ||
jne copy | jne copy | ||
− | + | ||
;;; --- open file --- | ;;; --- open file --- | ||
openFile filename, [handle], errorMsg, errorLen | openFile filename, [handle], errorMsg, errorLen | ||
Line 115: | Line 121: | ||
− | ;; | + | ;; ----------------------------------- |
− | + | ;; add your byte-examining loop below! | |
− | ;; | + | ;; you can access the bytes of the pixels as byte[ebx+pixelBuf] |
− | + | ;; and increment ebx in the loop to access successive bytes | |
− | ;; | + | ;; ----------------------------------- |
− | |||
− | ;; | ||
− | |||
− | |||
− | ;; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ;; ----------------------------------- | |
− | + | ;; mainFor: main section for reading secret bytes | |
− | + | ;; written by: Ellysha Raelen Recto | |
− | + | ;; registers modified: none | |
− | + | ;; ----------------------------------- | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | mainFor: | ||
+ | call decode1byte ; calls method to decode 1 byte of secret msg | ||
+ | cmp dl, 0 ; if 1 byte is zero, end of secret msg | ||
+ | je exit ; go to exit if end of msg is found | ||
+ | call printByte ; print byte | ||
+ | jmp mainFor ; decode more bytes if end of msg isnt found | ||
;; ----------------------------------- | ;; ----------------------------------- | ||
;; end of pixel loop area | ;; end of pixel loop area | ||
;; ----------------------------------- | ;; ----------------------------------- | ||
− | + | ||
;;; --- position file pointer back to beginning of file --- | ;;; --- position file pointer back to beginning of file --- | ||
seek0 [handle] | seek0 [handle] | ||
Line 166: | Line 152: | ||
;;; --- close file --- | ;;; --- close file --- | ||
close [handle] | close [handle] | ||
− | + | ||
;;; --- exit() --- | ;;; --- exit() --- | ||
exit: | exit: | ||
Line 204: | Line 190: | ||
ret 8 | ret 8 | ||
− | + | ;;; ------------------------------------------------------------- | |
− | ;;; -------------------------------------------------------- | + | ;;; decode1byte: decodes 1 byte of secret msg, first bit of the byte |
− | ;;; decode1byte: | + | ;;; is from the last bit of first 8th byte of image |
− | ;;; | + | ;;; registers modified: edx, ecx, eax, ebx |
− | + | ;;; ------------------------------------------------------------- | |
− | |||
− | |||
− | |||
− | ;;; registers modified: ebx | ||
− | ;;; ------------------------------------------------------- | ||
− | |||
decode1byte: | decode1byte: | ||
− | + | mov dl, 0 | |
− | + | mov ecx,7 ; reads first 7 bits | |
− | + | for: call read1bit ; call method to read 1 bit with shifting | |
− | + | dec ebx ; goes to next bit | |
− | mov | + | loop for |
− | + | mov al, byte[ebx+pixelBuf+7] ; reads last bit | |
− | + | and al, 1 | |
+ | or dl, al | ||
+ | add ebx, 15 | ||
+ | ret | ||
− | + | ;;; ------------------------------------------------------------- | |
− | + | ;;; printByte: prints 1 byte of secret msg | |
− | + | ;;; registers modified: edx, ecx, eax, ebx | |
− | + | ;;; ------------------------------------------------------------- | |
− | + | printByte: | |
− | ; | + | pushad ; pushes registers to stack |
− | + | mov byte[msg], dl ; moves byte read from image to msg | |
− | mov | + | mov eax, SYS_WRITE |
− | + | mov ebx, STDOUT | |
− | + | mov ecx, msg ; prints msg | |
− | + | mov edx, 1 | |
− | + | int 0x80 | |
− | + | popad ; retrieves values of registers from stack | |
+ | ret | ||
− | mov | + | ;;; ------------------------------------------------------------- |
− | and | + | ;;; read1bit: used to read a bit if it's one of the first 7 bits of |
− | or | + | ;;; a byte from the image |
− | + | ;;; registers modified: edx, eax | |
+ | ;;; ------------------------------------------------------------- | ||
+ | read1bit: | ||
+ | mov al, byte[ebx+pixelBuf+7] | ||
+ | and al, 1 ; get the last bit of byte from image | ||
+ | or dl, al ; saves the values of the bits from | ||
+ | ; the bytes of already read to dl | ||
+ | shl dl, 1 ; shifts bits to make room for next bit | ||
+ | ; to be read | ||
+ | ret | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | </ | + | </source> |
+ | </onlydft> | ||
+ | [[Category:CSC231]][[Category:nasm]] |