Advertisement
The_KGB

Convert chars in file to uppercase

Jul 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .section .data
  2. #######CONSTANTS########
  3.  #system call numbers
  4.  .equ SYS_OPEN, 5
  5.  .equ SYS_WRITE, 4
  6.  .equ SYS_READ, 3
  7.  .equ SYS_CLOSE, 6
  8.  .equ SYS_EXIT, 1
  9.  #options for open (look at
  10.  #/usr/include/asm/fcntl.h for
  11.  #various values.  You can combine them
  12.  #by adding them or ORing them)
  13.  #This is discussed at greater length
  14.  #in "Counting Like a Computer"
  15.  .equ O_RDONLY, 0
  16.  .equ O_CREAT_WRONLY_TRUNC, 03101
  17.  #standard file descriptors
  18.  .equ STDIN, 0
  19.  .equ STDOUT, 1
  20.  
  21. .equ STDERR, 2
  22.  #system call interrupt
  23.  .equ LINUX_SYSCALL, 0x80
  24.  .equ END_OF_FILE, 0  #This is the return value
  25.                       #of read which means we’ve
  26.                       #hit the end of the file
  27.  .equ NUMBER_ARGUMENTS, 2
  28. .section .bss
  29.  #Buffer - this is where the data is loaded into
  30.  #         from the data file and written from
  31.  #         into the output file.  This should
  32.  #         never exceed 16,000 for various
  33.  #         reasons.
  34.  .equ BUFFER_SIZE, 500
  35.  .lcomm BUFFER_DATA, BUFFER_SIZE
  36.  .section .text
  37. #STACK POSITIONS
  38. .equ ST_SIZE_RESERVE, 8
  39. .equ ST_FD_IN, -4
  40. .equ ST_FD_OUT, -8
  41. .equ ST_ARGC, 0
  42. .equ ST_ARGV_0, 4
  43. .equ ST_ARGV_1, 8
  44. .equ ST_ARGV_2, 12
  45.  
  46. .globl _start
  47. _start:
  48.  
  49. movl  %esp, %ebp
  50. subl  $ST_SIZE_RESERVE, %esp
  51. open_files:
  52. open_fd_in:
  53.  ###OPEN INPUT FILE###
  54.  #open syscall
  55.  movl  $SYS_OPEN, %eax
  56.  #input filename into %ebx
  57.  movl  ST_ARGV_1(%ebp), %ebx
  58.  #read-only flag
  59.  movl  $O_RDONLY, %ecx
  60.  #this doesn’t really matter for reading
  61.  movl  $0666, %edx
  62.  #call Linux
  63.  int   $LINUX_SYSCALL
  64. store_fd_in:
  65.  #save the given file descriptor
  66.  movl  %eax, ST_FD_IN(%ebp)
  67. open_fd_out:
  68.  ###OPEN OUTPUT FILE###
  69.  #open the file
  70.  movl  $SYS_OPEN, %eax
  71.  #output filename into %ebx
  72.  movl  ST_ARGV_2(%ebp), %ebx
  73.  #flags for writing to the file
  74.  movl  $O_CREAT_WRONLY_TRUNC, %ecx
  75.  #mode for new file (if it’s created)
  76.  movl  $0666, %edx
  77.  #call Linux
  78. int   $LINUX_SYSCALL
  79. store_fd_out:
  80.  #store the file descriptor here
  81.  movl  %eax, ST_FD_OUT(%ebp)
  82.  ###BEGIN MAIN LOOP###
  83. read_loop_begin:
  84.  ###READ IN A BLOCK FROM THE INPUT FILE###
  85.  movl  $SYS_READ, %eax
  86.  #get the input file descriptor
  87.  movl  ST_FD_IN(%ebp), %ebx
  88.  #the location to read into
  89.  movl  $BUFFER_DATA, %ecx
  90.  #the size of the buffer
  91.  movl  $BUFFER_SIZE, %edx
  92.  #Size of buffer read is returned in %eax
  93.  int   $LINUX_SYSCALL
  94.  ###EXIT IF WE’VE REACHED THE END###
  95.  #check for end of file marker
  96.  cmpl  $END_OF_FILE, %eax
  97.  #if found or on error, go to the end
  98.  jle   end_loop
  99. continue_read_loop:
  100. pushl $BUFFER_DATA
  101. pushl %eax
  102. call  convert_to_upper
  103. popl  %eax
  104. addl  $4, %esp
  105. #size of the buffer
  106.  movl  %eax, %edx
  107.  movl  $SYS_WRITE, %eax
  108.  #file to use
  109.  movl  ST_FD_OUT(%ebp), %ebx
  110.  #location of the buffer
  111.  movl  $BUFFER_DATA, %ecx
  112.  int   $LINUX_SYSCALL
  113. jmp   read_loop_begin
  114. end_loop:
  115. ###CLOSE THE FILES###
  116.  #NOTE - we don’t need to do error checking
  117.  #       on these, because error conditions
  118.  #       don’t signify anything special here
  119.  movl  $SYS_CLOSE, %eax
  120.  movl  ST_FD_OUT(%ebp), %ebx
  121.  int   $LINUX_SYSCALL
  122.  movl  $SYS_CLOSE, %eax
  123.  movl  ST_FD_IN(%ebp), %ebx
  124.  int   $LINUX_SYSCALL
  125.  ###EXIT###
  126.  movl  $SYS_EXIT, %eax
  127.  movl  $0, %ebx
  128.  int   $LINUX_SYSCALL
  129. ###CONSTANTS##
  130. #The lower boundary of our search
  131. .equ  LOWERCASE_A, 'a'
  132. #The upper boundary of our search
  133. .equ  LOWERCASE_Z, 'z'
  134. #Conversion between upper and lower case
  135. .equ  UPPER_CONVERSION, 'A' - 'a'
  136. ###STACK STUFF###
  137. .equ  ST_BUFFER_LEN, 8 #Length of buffer
  138. .equ  ST_BUFFER, 12    #actual buffer
  139. convert_to_upper:
  140.  pushl %ebp
  141.  movl  %esp, %ebp
  142.  ###SET UP VARIABLES###
  143.  movl  ST_BUFFER(%ebp), %eax
  144.  movl  ST_BUFFER_LEN(%ebp), %ebx
  145.  movl  $0, %edi
  146. #if a buffer with zero length was given
  147.  #to us, just leave
  148.  cmpl  $0, %ebx
  149.  je    end_convert_loop
  150. convert_loop:
  151.  #get the current byte
  152.  movb  (%eax,%edi,1), %cl
  153.  #go to the next byte unless it is between
  154.  #’a’ and ’z’
  155.  cmpb  $LOWERCASE_A, %cl
  156.  jl    next_byte
  157.  cmpb  $LOWERCASE_Z, %cl
  158.  jg    next_byte
  159.  #otherwise convert the byte to uppercase
  160.  addb  $UPPER_CONVERSION, %cl
  161.  #and store it back
  162.  movb  %cl, (%eax,%edi,1)
  163. next_byte:
  164.  incl  %edi
  165.  cmpl  %edi, %ebx
  166.  jne   convert_loop
  167. #next byte
  168. #continue unless
  169. #we’ve reached the
  170. #end
  171. end_convert_loop:
  172.  #no return value, just leave
  173.  movl  %ebp, %esp
  174.  popl  %ebp
  175.  ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement