; Jade Yu Cheng
; ICS 312
; Assignment 6 Exercise 2
; April 9, 2009
; This program prompts the user to enter a signed 32-bit integer. The program
; prints out the binary representation of the integer. Program also prints out
; the number of times the motif "1101" occurs in the binary representation of
; the entered number. For instance, the number "1101101001101" would contain
; the motif 3 times. The occurrences of the motif can overlap.
%include "asm_io.inc"
segment .data
msg1 db "Enter an integer: ", 0 ; msg1
msg2 db "The binary representation is: ", 0 ; msg2
msg3 db "The number of '1101' motifs is: ", 0 ; msg3
counter dd 0 ; motif counter
segment .bss
binary resb 32 ; space to store binary representation
input resd 1 ; space for the 32 bit input number
segment .text
global asm_main
asm_main:
enter 0,0 ; setup
pusha ; setup
;;; prompt the user the enter a number and treat it as a 32 bit number.
mov eax, msg1 ; print out the prompt message.
call print_string
call read_int ; read a number and store in eax
mov [input], eax ; store the input number into input
;;; convert the number to its binary and store it in binary..
convert:
mov ebx, binary ; ebx points to binary
add ebx, 31 ; ebx points to the last bit of binary
convert_loop:
cmp ebx, binary - 1 ; terminate when all 32 bits are written
je convert_loop_end
shr eax, 1 ; right shift by 1, equavilent as divide by 2
jc write_one ; if carry flag is set wirte 1 to ebx
mov [ebx], byte 0 ; otherwise wirte 0 to ebx
dec ebx ; decrement ebx to the next spot to write
jmp convert_loop
write_one:
mov [ebx], byte 1 ; write 1 to ebx
dec ebx ; decrement ebx to the next spot to write
jmp convert_loop
convert_loop_end:
;;; print out binary
print:
mov eax, msg2 ; print out the second message
call print_string
mov ebx, binary ; let ebx points to binary
print_loop:
cmp ebx, binary + 32; terminate when it reaches the end
je print_loop_end
mov eax,0 ; clean up eax
mov al, [ebx] ; move the bit in eax
call print_int ; print it out
inc ebx ; increment the pointer to the next spot
jmp print_loop
print_loop_end:
call print_nl ; print a blank line
;;; find out the incidence of the motif "1101" and print the number to the screen
mov ecx, [input] ; let ecx contain the value of input number
mov ebx, 13 ; let ebx contain the value of the motif
mov dl, 0 ; loop 29 times for a 4 bits long motif
find_loop:
cmp dl, 29
je find_loop_end
mov eax, ecx ; let eax temporally hold input number
and eax, 15 ; turn off the first 28 bits
shr ecx, 1 ; right shift ecx to examine the next 4 bits
inc edx ; increment loop counter
cmp eax, ebx ; compare motif with modified input
jne find_loop ; go to next loop without incrementing counter
inc dword [counter] ; increment counter if it's a match
jmp find_loop
find_loop_end:
;;; print out the message and counter saying how many "1101" motifs are found.
mov eax, msg3 ; print out message 3
call print_string
mov eax, [counter]
call print_int ; print out the counter value
call print_nl
;;; clean up code
popa ; cleanup
mov eax, 0 ; cleanup
leave ; cleanup
ret ; cleanup