Question

Create an assembly language program. The program asks to create a "raid" simulation program. So you take a string "This is a String!!" then you save it in four files. (First letter of the string in the first file and second in the second and third letter in the third file and the xor of the three in the fourth file and so forth). Then you delete one of the files and recreate the deleted file and the data of the file using the "xor" instruction with the rest of the third file. I am stuck on the recreating part. My program is reading the file but I think its reading it twice and the part I ask the user to delete the file. After deleting I need to use "xor" to recreate.

The following is the code from "raid.s" file
.section .data
msg: .asciz "Press enter key after deleting one of the files"
         .equ len2, str - msg
str: .asciz "This is a string!!"
strend: .equ len, strend - str
file:
file1: .asciz "file1.txt"
file2: .asciz "file2.txt"
file3: .asciz "file3.txt"
file4: .asciz "file4.txt"


.section .bss
fd:
    .lcomm fd1 4
    .lcomm fd2 4
    .lcomm fd3 4
    .lcomm fd4 4
    .lcomm parity 1
    .lcomm ffile 4 # address of failed filename, detected when opening read
    .lcomm first 6 # the 6 char from the first successfull read file
.section .text
.globl _start
_start:

movl $4, %eax       # 4 is the system call number for write
movl $1, %ebx       # 1 is fd for the console
movl $str, %ecx      # location of the string to write
movl $len, %edx      # count of bytes in string
int $0x80          # call the system kernel

movl $fd, %esi       # location of file descriptors array
movl $4, %ecx
movl $file, %ebx    # location of file names array
L1:
pushl %ecx          # save the value of the loop counter

movl $5, %eax       # open file is system call number 5
movl $001101, %ecx   # O_CREAT/O_TRUNC/O_WRONLY Octal 001101
movl $0644, %edx    # permissions: _rw_r__r__ Octal 0644
int $0x80

movl %eax, (%esi)    # save file descriptor ret from sys call
addl $4, %esi       # move to next fd in memory
addl $10, %ebx       # move to next filename in memory
popl %ecx            # restore value of counter
loop L1


movl $str, %edi      # location of string
movl $fd, %esi       # location of first file descriptor
movl $6, %ecx       # loop 6 times, write 3 char each time
L2:                  # write the xor of them in 4th file
pushl %ecx          # save the value of the loop counter
movl $fd, %esi       #location of first file descriptor
movl (%esi), %ebx    # fd needs to be in ebx
movl %edi, %ecx      # location of the string to write
movl (%ecx),%eax    # copy the byte to be written
pushl %eax          # save current parity value
movl $4, %eax       # 4 is sys call number for write
movl $1, %edx       # write one byte from string
int $0x80          # call the system kernel
popl %eax            # restore previous parity value


incl %ecx            # move to next byte of string
addl $4, %esi       #move to next element of fd array(fd[1])
movl (%esi), %ebx    #fd needs to be in ebx
xorl (%ecx),%eax    # xor next byte into parity
pushl %eax          # save current parity value
movl $4, %eax       # 4 is sys call number for write
int $0x80          # call the system kernel
popl %eax            # restore previous parity value


incl %ecx            # move to next byte of string
addl $4, %esi       #move to next element of fd array(fd[2])
movl (%esi), %ebx    #fd needs to be in ebx
xorl (%ecx),%eax    # xor next byte into parity
pushl %eax          # save current parity value
movl $4, %eax       # 4 is sys call number for write
int $0x80          # call the system kernel
popl %eax            # restore the parity byte


movb %al,parity
movl $parity,%ecx
addl $4, %esi       #move to next element of fd array(fd[3])
movl (%esi), %ebx    #fd needs to be in ebx
movl $4, %eax       # 4 is sys call number for write
int $0x80          # call the system kernel


addl $3,%edi         # next 3 characters
popl %ecx            # restore value of counter
loop L2             # expects ecx to have count

movl $4, %ecx       #close 4 files
movl $fd, %esi       # fd needs to be in ebx
L3:
movl (%esi), %ebx    #fd[i] needsto be in ebx
movl $6, %eax       # 6 is the system call number for close
int $0x80          # call the system kernel
addl $4, %esi       #call the system kernal.
loop L3


movl $4, %eax       # 4 is the system call number for write
movl $1, %ebx       # 1 is fd for the console
movl $str, %ecx      # location of the string to write
movl $len, %edx      # count of bytes in string
int $0x80          # call the system kernel

call display_

#pause while user deletes a file to simulte drive
movl $3, %eax       #3 is the system call number for read
movl $0, %ebx       # 0 is the fd for the console input
movl $str, %ecx      # location of the string to read into
movl $1, %edx       # count of bytes to read
int $0x80            # call the system kernel

#try opening the files for read
# we will know which failed by checking the fd afterwards
movl $fd, %esi       #location of file diescriptors array
movl $4, %ecx
movl $file, %ebx    # location of file names array
L4:
pushl %ecx          #save the value of the loop counter

movl $5, %eax       # open file is sustem call number 5
movl $00, %ecx       # O_rDONLY Octal 00
movl $0644, %edx    # permissions: _rw_r__r__ Octal 0644
int $0x80

# Check for negative value in eax to mean dive failed
testl %eax, %eax    #and eax to itself to set flags
jns L5   

#ebx is pointing to the failed file name, save it
movl %ebx, %ecx      # file name pointer
movl %ecx, ffile    # save to ffile(failed filename pointer)

L5:                  #file opened OK, no need to save name
movl %eax, (%esi)    # save file descriptor ret from sys call
addl $4, %esi       # move to next fd in memory
addl $10, %ebx       # move to next filename in memory
popl %ecx            # restore value of counter
loop L4


call swap_          # move fd of fail dto 4th postion
#call open4th_       # open it for writing (the other 3 are alrady open for reading )
#push parameteres on stack right to left
pushl $fd1            # if this the bad one 20 up from ebp
pushl $fd2            #one of the good ones.. 16 up from ebp
pushl $fd3            #12 up from ebp
pushl $fd4            # 8 up from ebp

call rebuild_

movl $1, %eax       # 1 is system call numbe for exit
movl $0, %ebx       # 0 indicates successful completion
int $0x80

# function to open the 4th file for writing
# assume that swap identified the file name that needs reqriting

# function to sawp order of fd array, so that failed drive is 4th
# all have been set from opening for read, if one failed, it is negative
# use index of failing fd to find name of file, write to ffile
.globl swap_
swap_:
pushl %esi
pushl %edi
pushl %ecx
pushl %ebx
movl $fd+12, %edi    # this the address of 4th fd
movl $fd, %esi
movl $3, %ecx         # asume only one can fail , if 1-3 ok, assume 4th failed
L6:
movl (%esi), %eax    # save current fd into eax
testl %eax, (%esi)    # check for negative
jge L7
#swap
movl (%edi), %ebx
movl %ebx, (%esi)
movl %eax, (%edi)
L7: # no swap needed
movl (%esi), %eax # temp
addl $4, %esi
loop L6

popl %ebx
popl %ecx
popl %edi
popl %esi
ret

#function to rebuild the fourth file in fd array with first 3
#assuming first 3 fd are good and open for reading, 4th is not open
rebuild_:
pushl %ebp #save previous frame pointer
movl %esp, %ebp # creates this function stack frame pointer
# save registers modified here
#movl 8(%ebp), fd1 #first parameter: f1
#movl 12(%ebp), fd2 # second parameter:f2
#movl 16 (%ebp), fd3 # third parameter:f3
#movl 20 (%ebp), fd4 # fourth parameter:f3
movl $3, %eax       #3 is the system call number for read
movl fd1, %ebx      # A fd for a file to read
movl $first, %ecx # location of the string to read into
movl $6, %edx      # count of bytes to read
int $0x80 # call the system kernel
# restore registers that were modified
popl %ebp   #restore previous routines frame pointer
ret

#function to dosplay info about failing drive
.globl display_
display_:
movl $4, %eax   # 4 is the system call number for write
movl $1, %ebx   # 1 is fd for the console (stdout)
movl $msg, %ecx # location of the string to write
movl $len2, %edx # count of bytes in string
int $0x80       # call the system kernel
ret

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#pause while user deletes a file to simulte drive
movl $3, %eax       #3 is the system call number for read
movl $0, %ebx       # 0 is the fd for the console input
movl $str, %ecx      # location of the string to read into
movl $1, %edx       # count of bytes to read
int $0x80            # call the system kernel

#try opening the files for read
# we will know which failed by checking the fd afterwards
movl $fd, %esi       #location of file diescriptors array
movl $4, %ecx
movl $file, %ebx    # location of file names array
L4:
pushl %ecx          #save the value of the loop counter

movl $5, %eax       # open file is sustem call number 5
movl $00, %ecx       # O_rDONLY Octal 00
movl $0644, %edx    # permissions: _rw_r__r__ Octal 0644
int $0x80

# Check for negative value in eax to mean dive failed
testl %eax, %eax    #and eax to itself to set flags
jns L5   

#ebx is pointing to the failed file name, save it
movl %ebx, %ecx      # file name pointer
movl %ecx, ffile    # save to ffile(failed filename pointer)

L5:                  #file opened OK, no need to save name
movl %eax, (%esi)    # save file descriptor ret from sys call
addl $4, %esi       # move to next fd in memory
addl $10, %ebx       # move to next filename in memory
popl %ecx            # restore value of counter
loop L4...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Get help from a qualified tutor
Live Chats