0
votes

i was doing a program in NASM(x86 assembly), in which user is asked to enter three 32 bit hex numbers(8 digit), which are further stored in an array and the program shows the number which is largest of them all. The program works fine, i.e. it shows the largest of the three numbers. But the problem is, that it shows only 16 bit (4 digit number) as output. For example, if i give three numbers as 11111111h,22222222h and 10000000h, the output comes out to be only 2222. This is the code.

    section .data

    msg db "Enter the number    :   ",10d,13d
    msglen equ $-msg
    show db "The greatest number is :   ",10d,13d
    showlen equ $-show

    %macro display 2
        mov eax,4
        mov ebx,1
        mov ecx,%1
        mov edx,%2
            int 80h
    %endmacro

    %macro input 2
        mov eax,3
        mov ebx,0
        mov ecx,%1
        mov edx,%2
            int 80h
    %endmacro

    section .bss
          large resd 12
          num   resd    3

    section .text
    global _start
    _start:

    mov esi,num
    mov edi,3

    ; Now taking input  
    nxt_num:
              display msg,msglen
                    input esi,12

              add esi,12
              dec edi
              jnz nxt_num

              mov esi,num
              mov edi,3

    add:      mov eax,[esi]
              jmp check

    next:     add esi,12
              mov ebx,[esi]
              CMP ebx,eax
              jg add

    check:    dec edi       
              jnz next

    mov [large],eax
    display show,showlen
    display large,12    

    ;exit
    mov eax,1
    mov ebx,0 
        int 80h

I even tried changing reserved size of array from doubly byte to quad byte. But the result remains the same.

Also, when i execute the same code in NASM x86_64 assembly, only with the registers and the system calls changed (i.e. eax to rax, ebx to rcx, int 80h to syscall, etc) the output comes out to of 32 bits(8 digits). Why so? I need help. Thank you. :)

2

2 Answers

0
votes

In you little program , you're trying to move the Qword into a 32-bit register which can hold just 4bytes (DWord). Based on your response to Gunner I guess you're misunderstanding this concept. Actually each byte is represented by 8bits.

a word is 2 bytes (16 bits)

a dword is 4 bytes (32 bits) which is the size of a register in a x86 arch.

So whenever you take a byte , its binary equivalent has always an 8bits size. So the binary equivalent of "FF" in hex is 00001111.

In your program just try to print your number as a string instead of printing it through a register, you can simply do that by using the pointer to the memory address where you number is stored or simply by printing the input using printf.

P.S : the string should be in ASCII , so to display 11111111 it should be in memory as following 3131313131313131 .

0
votes

The output 2222 is correct for a 32 bit register. Each number is 8 bits, 4 numbers = 8 * 4 = 32, the max a 32 bit register can hold. This is why if you change to 64 bit registers, the full number is printed. You will need to change the displayed number into a string to display the full number.