Facebook
From Corrupt Porcupine, 3 Years ago, written in Plain Text.
This paste is a reply to Untitled from Capacious Gibbon - view diff
Embed
Download Paste or View Raw
Hits: 108
  1. #----------------------------------------------------------------
  2. # Program lab_3.s - Architektury Komputerów
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile: as --defsym FUNC_V1=1 -o lab_3.o lab_3.s
  6. #  To link:     ld -o lab_3 lab_3.o
  7. #  To run:      ./lab_3
  8. #
  9. #----------------------------------------------------------------
  10.  
  11.     .equ    write_64, 0x01    # write data to file function
  12.     .equ    exit_64, 0x3C    # exit program function
  13.     .equ    stdout, 0x01    # handle to stdout
  14.  
  15.     .data
  16.    
  17. hex_str:
  18.     .ascii    "00 "              # hex code string
  19. big_hex_str:
  20.     .ascii    "0x0000000000000000"    # big hex code string
  21. new_line:
  22.     .ascii    "\n"               # new line
  23. tmp:
  24.     .byte    0                   # tmp variable
  25.  
  26.     .ifdef FUNC_V2
  27. hex_digit:
  28.     .byte '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
  29.     .endif
  30.  
  31.     .ifdef FUNC_V3
  32. hex_digits:
  33.     .ascii "00","01","02","03","04","05","06","07"
  34.     .ascii "08","09","0A","0B","0C","0D","0E","0F"
  35.     .ascii "10","11","12","13","14","15","16","17"
  36.     .ascii "18","19","1A","1B","1C","1D","1E","1F"
  37.     .ascii "20","21","22","23","24","25","26","27"
  38.     .ascii "28","29","2A","2B","2C","2D","2E","2F"
  39.     .ascii "30","31","32","33","34","35","36","37"
  40.     .ascii "38","39","3A","3B","3C","3D","3E","3F"
  41.     .ascii "40","41","42","43","44","45","46","47"
  42.     .ascii "48","49","4A","4B","4C","4D","4E","4F"
  43.     .ascii "50","51","52","53","54","55","56","57"
  44.     .ascii "58","59","5A","5B","5C","5D","5E","5F"
  45.     .ascii "60","61","62","63","64","65","66","67"
  46.     .ascii "68","69","6A","6B","6C","6D","6E","6F"
  47.     .ascii "70","71","72","73","74","75","76","77"
  48.     .ascii "78","79","7A","7B","7C","7D","7E","7F"
  49.     .ascii "80","81","82","83","84","85","86","87"
  50.     .ascii "88","89","8A","8B","8C","8D","8E","8F"
  51.     .ascii "90","91","92","93","94","95","96","97"
  52.     .ascii "98","99","9A","9B","9C","9D","9E","9F"
  53.     .ascii "A0","A1","A2","A3","A4","A5","A6","A7"
  54.     .ascii "A8","A9","AA","AB","AC","AD","AE","AF"
  55.     .ascii "B0","B1","B2","B3","B4","B5","B6","B7"
  56.     .ascii "B8","B9","BA","BB","BC","BD","BE","BF"
  57.     .ascii "C0","C1","C2","C3","C4","C5","C6","C7"
  58.     .ascii "C8","C9","CA","CB","CC","CD","CE","CF"
  59.     .ascii "D0","D1","D2","D3","D4","D5","D6","D7"
  60.     .ascii "D8","D9","DA","DB","DC","DD","DE","DF"
  61.     .ascii "E0","E1","E2","E3","E4","E5","E6","E7"
  62.     .ascii "E8","E9","EA","EB","EC","ED","EE","EF"
  63.     .ascii "F0","F1","F2","F3","F4","F5","F6","F7"
  64.     .ascii "F8","F9","FA","FB","FC","FD","FE","FF"
  65.     .endif
  66.  
  67. varb:    .byte    190                    # byte value (2 chars)
  68. varw:    .word    51966                  # word value (4 chars)
  69. varl:    .long    3735927486     # long value (8 chars)
  70. varq:    .quad    18369548392556473261    # quad value (16 chars)
  71.  
  72. #----------------------------------------------------------------
  73.  
  74.     .text
  75.     .global _start
  76.  
  77.     .macro disp_str_64 file_id, address, length
  78.     mov $write_64, %rax
  79.     mov \file_id, %rdi
  80.     mov \address, %rsi
  81.     mov \length, %rdx
  82.     syscall
  83.     .endm
  84.  
  85.     .macro exit_prog_64 exit_code
  86.     mov $exit_64, %rax
  87.     mov \exit_code, %rdi
  88.     syscall
  89.     .endm
  90.  
  91. #----------------------------------------------------------------
  92.    
  93. _start:
  94.     mov $256, %r15       # loop counter = 256;
  95.     xor %r14, %r14       # number = 0;
  96.  
  97. again:
  98.     mov %r14b, %al
  99.     call byte2hex        # ax = hex_code( al );
  100.     movw %ax, hex_str
  101.  
  102.     disp_str_64 $stdout, $hex_str, $3
  103.  
  104.     inc %r14     # number++;
  105.  
  106.     mov %r14, %rax       # if( !(number % 16) ) newline();
  107.     and $15, %rax
  108.     jnz skip
  109.     disp_str_64 $stdout, $new_line, $1
  110. skip:
  111.  
  112.     dec %r15     # counter--;
  113.     jnz again
  114.  
  115.     disp_str_64 $stdout, $new_line, $1
  116.  
  117.     movb varb, %al               # convert byte to hex string
  118.     movb $1, %cl                 # it's byte, so size = 1
  119.     mov $big_hex_str+2, %rdi    # address of most significant digit of least significant byte
  120.     call num2hex
  121.     disp_str_64 $stdout, $big_hex_str, $4    # 0x + 2 digits
  122.     disp_str_64 $stdout, $new_line, $1
  123.  
  124.     movw varw, %ax               # convert word to hex string
  125.     movb $2, %cl                 # it's word, so size = 2
  126.     mov $big_hex_str+4, %rdi    # address of most significant digit of least significant byte
  127.     call num2hex
  128.     disp_str_64 $stdout, $big_hex_str, $6    # 0x + 4 digits
  129.     disp_str_64 $stdout, $new_line, $1
  130.  
  131.     movl varl, %eax              # convert long to hex string
  132.     movb $4, %cl                 # it's long, so size = 4
  133.     mov $big_hex_str+8, %rdi    # address of most significant digit of least significant byte
  134.     call num2hex
  135.     disp_str_64 $stdout, $big_hex_str, $10    # 0x + 8 digits
  136.     disp_str_64 $stdout, $new_line, $1
  137.  
  138.     movq varq, %rax              # convert quad to hex string
  139.     movb $8, %cl                 # it's quad, so size = 8
  140.     mov $big_hex_str+16, %rdi    # address of most significant digit of least significant byte
  141.     call num2hex
  142.     disp_str_64 $stdout, $big_hex_str, $18    # 0x + 16 digits
  143.     disp_str_64 $stdout, $new_line, $1
  144.  
  145. theend:
  146.     exit_prog_64 $0      # exit program
  147.  
  148. #----------------------------------------------------------------
  149. # num2hex - converts number to hexadecimal string
  150. #    Arguments:    %rax - number (%al, %ax, %eax)
  151. #                %cl - size of number (1,2,4,8)
  152. #                %rdi - address (where to put hex digits)
  153. #----------------------------------------------------------------
  154.  
  155.     .type num2hex, @function
  156.  
  157. num2hex:
  158.     mov %rax, %rdx       # store original value in %rdx
  159. next_byte:
  160.     call byte2hex        # convert byte in %al to two hexdigits (in %ax)
  161.     movw %ax, (%rdi)    # store digits in memory (string)
  162.     sub $2, %rdi         # move two chars to the left
  163.     shr $8, %rdx         # shift original value right
  164.     mov %rdx, %rax       # copy value to %rax
  165.     dec %cl              # size--;
  166.     jnz next_byte        # more bytes to convert
  167.  
  168.     ret
  169.  
  170. #----------------------------------------------------------------
  171. # byte2hex - converts byte to hexadecimal number (first version)
  172. #        Argument:    %al - byte to convert
  173. #        Returns:    %ax - two hex digits
  174. #----------------------------------------------------------------
  175.  
  176.     .ifdef FUNC_V1
  177.  
  178.     .type byte2hex,@function
  179.  
  180. byte2hex:
  181.     MOVB    %al, tmp
  182.  
  183.     ANDB    $0x0F,%al            # first nibble
  184.     CMPB    $10,%al
  185.     JB    digit1
  186.     ADDB    $('A'-10),%al
  187.     JMP    insert1
  188. digit1:
  189.     ADDB    $'0',%al
  190. insert1:
  191.     MOVB    %al,%ah
  192.  
  193.     MOVB    tmp,%al      # second nibble
  194.     SHR    $4,%al
  195.     CMPB    $10,%al
  196.     JB    digit2
  197.     ADDB    $('A'-10),%al
  198.     JMP    insert2
  199. digit2:
  200.     ADDB    $'0',%al
  201. insert2:
  202.     RET
  203.     .endif
  204.  
  205. #----------------------------------------------------------------
  206. # byte2hex - converts byte to hexadecimal number (second version)
  207. #        Argument:    %al - byte to convert
  208. #        Returns:    %ax - two hex digits
  209. #----------------------------------------------------------------
  210.  
  211.     .ifdef FUNC_V2
  212.  
  213.     .type byte2hex,@function
  214.  
  215. byte2hex:
  216.     MOVB    %al, tmp
  217.    
  218.     ANDB    $0x0F, %al           # first nibble
  219.     MOVZX    %al, %rbx           # rbx = al; zeros in empty space
  220.     MOVB    hex_digit(%rbx), %ah    # ah = hex_digit[ rbx ]
  221.  
  222.     MOVB    tmp, %al     # second nibble
  223.     SHR    $4, %al
  224.     MOVZX    %al, %rbx           # rbx = al; zeros in empty space
  225.     MOVB    hex_digit(%rbx), %al    # al = hex_digit[ rbx ]
  226.     RET
  227.  
  228.     .endif
  229.  
  230. #----------------------------------------------------------------
  231. # byte2hex - converts byte to hexadecimal number (third version)
  232. #        Argument:    %al - byte to convert
  233. #        Returns:    %ax - two hex digits
  234. #----------------------------------------------------------------
  235.  
  236.     .ifdef FUNC_V3
  237.  
  238.     .type byte2hex,@function
  239.  
  240. byte2hex:
  241.     MOVZX    %al, %rbx                   # rbx = al; zeros in empty space
  242.     MOVW    hex_digits(,%rbx,2), %ax    # ah = hex_digit[ rbx ]
  243.     RET
  244.     .endif

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Jittery Dormouse text 3 Years ago.