Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Написать программу nasm 32 bit printf scanf с использованием циклов. Пользователь вводит…

  1. User

    Написать программу nasm 32 bit printf scanf с использованием циклов. Пользователь вводит число в десятичной системе счисления, необходимо вывести это число в двоичной системе счисления в обратном порядке.

    Пример:

    126 0111111

  2. Assistant
    ; using nasm version 2.13.02
    ; nasm -f elf reverse_bits.asm
    ; ld -m elf_i386 -o reverse_bits reverse_bits.o
    ; ./reverse_bits
    
    ; printf and scanf formats
    section .data
    input_format: db "%d", 0
    output_format: db "%d", 0
    
    ; variables
    section .bss
    input_num: resd 1
    output_num: resd 1
    
    ; importing external functions
    section .text
    extern printf
    extern scanf
    extern exit
    global _start
    
    _start:
    
        ; reading input number
        push input_num
        push input_format
        call scanf
        add esp, 8
    
        ; initialize variables
        mov ecx, 0  ; counter
        mov eax, 0  ; result
        mov ebx, [input_num] ; storing input number in ebx
    
    reverse_loop:
        ; taking bit by bit
        mov edx, 1
        and edx, ebx
    
        ; adding bit to the result
        add eax, edx
    
        ; shifting input number to the right and result to the left
        shr ebx, 1
        shl eax, 1
    
        ; increase counter and check if all bits are processed
        inc ecx
        cmp ecx, 32
        jl reverse_loop
    
        ; shifting result to the right to eliminate the last left shift
        shr eax, 1
    
        ; storing the result in the output_num
        mov [output_num], eax
    
        ; printing the result
        push output_num
        push output_format
        call printf
        add esp, 8
    
        ; exit the program
        push 0
        call exit
    
  3. User

    Написать программу nasm 32 bit printf scanf с использованием циклов. Пользователь вводит число в десятичной системе счисления, необходимо вывести это число в двоичной системе счисления в обратном порядке.

    Пример:

    126 0111111

  4. Assistant
    ; using nasm version 2.13.02
    ; nasm -f elf reverse_bits.asm
    ; ld -m elf_i386 -o reverse_bits reverse_bits.o
    ; ./reverse_bits
    
    ; printf and scanf formats
    section .data
    input_format: db "%d", 0
    output_format: db "%c", 0
    
    ; variables
    section .bss
    input_num: resd 1
    
    ; importing external functions
    section .text
    extern printf
    extern scanf
    extern exit
    global _start
    
    _start:
    
        ; reading input number
        push input_num
        push input_format
        call scanf
        add esp, 8
    
        ; initialize variables
        mov ecx, 0  ; counter
        mov ebx, [input_num] ; storing input number in ebx
    
    reverse_loop:
        ; taking bit by bit (0 or 1)
        mov edx, 1
        and edx, ebx
    
        ; converting bit to ASCII character
        add edx, '0'
    
        ; printing character
        push edx
        push output_format
        call printf
        add esp, 8
    
        ; shifting input number to the right
        shr ebx, 1
    
        ; increment counter and check if all bits are processed
        inc ecx
        cmp ecx, 32
        jl reverse_loop
    
        ; exit the program
        push 0
        call exit
    

    Эта версия программы выводит каждый бит отдельно, без сохранения в промежуточной переменной. Таким образом, результат будет выводиться в виде строк из символов '0' и '1'.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.