Facebook
From Jittery Dormouse, 2 Years ago, written in Plain Text.
This paste is a reply to Re: Untitled from Corrupt Porcupine - view diff
Embed
Download Paste or View Raw
Hits: 79
  1. #----------------------------------------------------------------
  2. # Program LAB_4.S - Architektury Komputerów
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile: as -o lab_4.o lab_4.s
  6. #  To link:    ld -o lab_4 lab_4.o
  7. #  To run:     ./lab_4
  8. #
  9. #----------------------------------------------------------------
  10.  
  11.         .equ    write_64,       1
  12.         .equ    exit_64,        60
  13.         .equ    stdout,         1
  14.  
  15. #----------------------------------------------------------------
  16.  
  17.         .data
  18.  
  19. vector:                                 # vector of items
  20.         .long   10,70,50,90,60,80,40,20,0,30,98,78
  21. count:                                  # count of items
  22.         .quad   ( . - vector ) >> 2
  23. item:  
  24.         .string "Item "
  25. line_no:       
  26.         .string "   "
  27. itemval:       
  28.         .string " = "
  29. number:
  30.         .string "     \n"
  31. FL_text:       
  32.         .string "\nFrom first to last:\n"
  33. LF_text:       
  34.         .string "\nFrom last to first:\n"
  35. dataend:
  36.  
  37.         .equ    item_len, FL_text - item
  38.         .equ    FL_len, LF_text - FL_text
  39.         .equ    LF_len, dataend - LF_text
  40.  
  41. #----------------------------------------------------------------
  42.  
  43.         .text
  44.         .global _start
  45.  
  46.         .macro disp_str_64 file_id, address, length
  47.         mov $write_64, %rax
  48.         mov \file_id, %rdi
  49.         mov \address, %rsi
  50.         mov \length, %rdx
  51.         syscall
  52.         .endm
  53.  
  54.         .macro exit_prog_64 exit_code
  55.         mov $exit_64, %rax
  56.         mov \exit_code, %rdi
  57.         syscall
  58.         .endm
  59.  
  60. #----------------------------------------------------------------
  61.        
  62. _start:
  63.         disp_str_64 $stdout, $FL_text, $FL_len  # display message
  64.  
  65.         CALL    disp_vector_FL                  # display content of vector
  66.  
  67.         disp_str_64 $stdout, $LF_text, $LF_len  # display message
  68.  
  69.         CALL    disp_vector_LF                  # display content of vector
  70.  
  71.         exit_prog_64 $0                         # exit program
  72.  
  73. #----------------------------------------------------------------
  74. #
  75. #       Function:       disp_vector_FL
  76. #       Parameters:     none
  77. #
  78.         .type disp_vector_FL,@function
  79. disp_vector_FL:
  80.         MOV     count,%rcx              # data count
  81.         XOR     %rsi,%rsi               # data index
  82. next_item:
  83.         MOV     vector(,%rsi,4),%ebx    # get data
  84.         CALL    make_string             # convert to string
  85.         push %rcx
  86.         push %rsi
  87.         disp_str_64 $stdout, $item, $item_len   # display prepared string
  88.         pop %rsi
  89.         pop %rcx
  90.         INC     %rsi                    # next element
  91.         LOOP    next_item               # { rcx--; if( rcx ) goto next_item }
  92.  
  93.         RET                             # return to main program
  94. #----------------------------------------------------------------
  95. #
  96. #       Function:       disp_vector_LF
  97. #       Parameters:     none
  98. #
  99.         .type disp_vector_LF,@function
  100. disp_vector_LF:
  101.         MOV     count,%rcx              # data count
  102.         MOV     %rcx,%rsi               # data index
  103.         DEC     %rsi                    # data index--
  104. prev_item:
  105.         MOV     vector(,%rsi,4),%ebx    # get data
  106.         CALL    make_string             # convert to string
  107.         push %rcx
  108.         push %rsi
  109.         disp_str_64 $stdout, $item, $item_len   # display prepared string
  110.         pop %rsi
  111.         pop %rcx
  112.         DEC     %rsi                    # previous element
  113.         LOOP    prev_item               # { rcx--; if( rcx ) goto prev_item }
  114.  
  115.         RET                             # return to main program
  116. #----------------------------------------------------------------
  117. #
  118. #       Function:       make_string
  119. #       Parameters:     %esi - index of element
  120. #                       %ebx - value of element
  121. #
  122.         .type make_string,@function
  123. make_string:
  124. #       MOVL $0x20202020, number
  125.         MOVW $0x2020, line_no
  126.         MOV     %esi,%eax               # convert index of vector element t
  127.         MOV     $line_no + 2,%rdi
  128.         CALL    num2dec
  129.         MOV     %ebx,%eax               # convert value of vector element to
  130.         MOV     $number + 4,%rdi
  131.         CALL    num2dec
  132.  
  133.         RET                             # return to disp_vector function
  134. #----------------------------------------------------------------
  135. #
  136. #       Function:       num2dec
  137. #       Parameters:     %eax - value
  138. #                       %rdi - address of last character
  139. #
  140.         .type num2dec,@function
  141. num2dec:
  142.         PUSH    %rbx            # save register on stack
  143.         PUSH    %rdx            # save register on stack
  144.         MOV     $10,%ebx        # divisor in EBX, dividend in EAX
  145. nextdig:                       
  146.         XOR     %edx,%edx       # EDX = 0
  147.         DIV     %ebx            # EDX:EAX div EBX
  148.         ADD     $'0',%dl        # convert remainder (in EDX) to character
  149.         MOV     %dl,(%rdi)      # *(RDI) = character (decimal digit)
  150.         CMP     $0,%eax         # quotient in EAX
  151.         JZ      empty
  152.         DEC     %rdi            # RDI--
  153.         JMP     nextdig        
  154. empty:         
  155.         POP     %rdx            # restore register from stack
  156.         POP     %rbx            # restore register from stack
  157.  
  158.         RET                     # return to make_string function
  159. #----------------------------------------------------------------

Replies to Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Untitled Mature Crane text 2 Years ago.