Facebook
From Capacious Gibbon, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 85
  1. #----------------------------------------------------------------
  2. # Program lab_2.s - Architektury komputerów
  3. #----------------------------------------------------------------
  4. #
  5. #  To compile: as -o lab_2.o lab_2.s
  6. #  To link:    ld -o lab_2 lab_2.o
  7. #  To run:     ./lab_2
  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.  
  16.         .data
  17.        
  18. hex_str:
  19.         .ascii  "00 "           # hex code string
  20. new_line:
  21.         .ascii  "\n"            # new line
  22. tmp:
  23.         .byte   0               # tmp variable
  24.  
  25.  
  26.         .text
  27.         .global _start
  28.  
  29.         .macro disp_str_64 file_id, address, length
  30.         mov $write_64, %rax
  31.         mov \file_id, %rdi
  32.         mov \address, %rsi
  33.         mov \length, %rdx
  34.         syscall
  35.         .endm
  36.  
  37.         .macro exit_prog_64 exit_code
  38.         mov $exit_64, %rax
  39.         mov \exit_code, %rdi
  40.         syscall
  41.         .endm
  42.        
  43. _start:
  44.         mov $256, %r15          # loop counter = 256;
  45.         xor %r14, %r14          # number = 0;
  46.  
  47. again:
  48.         mov %r14b, %al
  49.         call num2hex            # ax = hex_code( al );
  50.         movw %ax, hex_str
  51.  
  52.         disp_str_64 $stdout, $hex_str, $3
  53.         inc %r14
  54.         mov %r14, %rax           #if( !(number % 16) ) newline();
  55.         and $15, %rax
  56.         jnz skip
  57.         disp_str_64 $stdout, $new_line, $1
  58.         #inc %r14
  59. skip:
  60.  
  61.         #inc %r14               # number++;
  62.  
  63.         dec %r15                # counter--;
  64.         jnz again
  65.  
  66. theend:
  67.         exit_prog_64 $0         # exit program
  68.  
  69.  
  70. #----------------------------------------------------------------
  71. # num2hex - converts byte to hexadecimal number
  72. #----------------------------------------------------------------
  73.  
  74.         .type num2hex,@function
  75.  
  76. num2hex:
  77.         MOVB    %al,tmp
  78.  
  79.         MOVB    tmp,%al         # first nibble
  80.         ANDB    $0x0F,%al
  81.         CMPB    $10,%al
  82.         JB      digit1
  83.         ADDB    $('A'-10),%al
  84.         JMP     insert1
  85. digit1:
  86.         ADDB    $'0',%al
  87. insert1:
  88.         MOVB    %al,%ah
  89.  
  90.         MOVB    tmp,%al         # second nibble
  91.         SHR     $4,%al
  92.         CMPB    $10,%al
  93.         JB      digit2
  94.         ADDB    $('A'-10),%al
  95.         JMP     insert2
  96. digit2:
  97.         ADDB    $'0',%al
  98. insert2:
  99.         RET

Replies to Untitled rss

Title Name Language When
Re: Untitled Corrupt Porcupine text 4 Years ago.