Advertisement
mdgaziur001

bigbrain

Jun 20th, 2023
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .section .text
  2. .global main
  3. .intel_syntax noprefix
  4.  
  5. convert:
  6.   push rbp
  7.   mov rbp, rsp
  8.   sub rsp, 16
  9.  
  10.   lea rdi, qword ptr [rip+.fmt_scanf]
  11.   lea rsi, qword ptr [rip+input]
  12.   xor rax, rax
  13.   call scanf@plt
  14.  
  15.   lea rdi, qword ptr [rip+.fmt_printf_message]
  16.   xor rax, rax
  17.   call printf@plt
  18.  
  19.   # copy 'n' to rdx
  20.   mov r13, [rip+input]
  21.  
  22.   // bool display = false;
  23.   mov r14, 0
  24.  
  25.   // int i = 63
  26.   mov r12, 63
  27.   .loop:
  28.     // int bit = r13;
  29.     mov rsi, r13
  30.    
  31.     // bit = (bit >> i) & 1;
  32.     mov rcx, r12
  33.     shr rsi, cl
  34.     and rsi, 1
  35.    
  36.     // if (bit)
  37.     cmp rsi, 1
  38.     je .set_display_to_true
  39.     jne .check_if_display
  40.  
  41.     // display = true
  42.     .set_display_to_true:
  43.       mov r14, 1
  44.    
  45.     // if (display)
  46.     .check_if_display:
  47.       cmp r14, 1
  48.       jne .decrement
  49.  
  50.     // printf("%d", bit)
  51.     .show_bit:
  52.       lea rdi, qword ptr [rip+.fmt_printf]
  53.       xor rax, rax
  54.       call printf@plt
  55.  
  56.     .decrement:
  57.       // i--
  58.       sub r12, 1
  59.  
  60.     // i >= 0
  61.     cmp r12, 0
  62.     jge .loop
  63.  
  64.   lea rdi, qword ptr [rip+.newline]
  65.   xor rax, rax
  66.   call printf@plt
  67.  
  68.   add rsp, 16
  69.   pop rbp
  70.  
  71.   ret
  72.  
  73. main:
  74.   push rbp
  75.   mov rbp, rsp
  76.   sub rsp, 16
  77.  
  78.   .conversion:
  79.     call convert
  80.   jmp .conversion
  81.  
  82.   add rsp, 16
  83.   pop rbp
  84.  
  85.   # return 0
  86.   xor rax, rax
  87.   ret
  88.  
  89. .section .rodata
  90. .fmt_scanf:
  91.   .asciz "%llu"
  92.  
  93. .fmt_printf_message:
  94.   .asciz "Binary: "
  95.  
  96. .fmt_printf:
  97.   .asciz "%d"
  98.  
  99. .newline:
  100.   .asciz "\n"
  101.  
  102. .section .bss
  103. input: .zero 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement