Monday, 4 August 2014

NASM: program to find largest number from an array of 32-bit numbers(hard-coded)

;     Find largest number from the array of 32-bit numbers.
;    Program search for largest number and display the same.

section .data
    larg_msg db 'Largest Number is::'
    larg_len: equ $-larg_msg

    nwline db 10

    array dd 0fa100001h,0b2000002h,0ffffffffh,0d400004h, 0500005h        ;array elements
    arrcnt dd 05h

section .bss
    dnum_buff resb 8
    large resd 1

%macro dispmsg 2
    mov eax,4    ;System call for write
    mov ebx,1    ;standard output stream
    mov ecx,%1    ;message start address
    mov edx,%2    ;message length
    int 80h

section .text
    global _start
        global break
    mov esi,0
    mov ecx,[arrcnt]
break1:    mov eax,0

lup1:    cmp eax,[array+esi*4]    ;Compare accumulator with array element
    ja lskip1        ;If accumulator is greater go to skip
    mov eax,[array+esi*4]    ;Else move array element in accumulator
lskip1:    inc esi            ;Point to next element
    loop lup1

    mov [large],eax

    dispmsg larg_msg,larg_len
    mov ebx,[large]
    call disp_num

    dispmsg nwline,1

exit:    mov eax,01
    mov ebx,0
    int 80h

    mov edi,dnum_buff    ;point esi to buffer

    mov ecx,8        ;load number of digits to display 
    rol ebx,4        ;rotate number left by four bits
    mov dl,bl        ;move lower byte in dl
    and dl,0fh        ;mask upper digit of byte in dl
    add dl,30h        ;add 30h to calculate ASCII code
    cmp dl,39h        ;compare with 39h
    jbe dispskip1        ;if less than 39h akip adding 07 more 
    add dl,07h        ;else add 07
    mov [edi],dl        ;store ASCII code in buffer
    inc edi            ;point to next byte
    loop dispup1        ;decrement the count of digits to display
                ;if not zero jump to repeat
    dispmsg dnum_buff,8

;[apcoer@localhost ~]$ nasm -f elf64 32h.asm
;[apcoer@localhost ~]$ ld -o 32h 32h.o
;[apcoer@localhost ~]$ ./32h
;Largest Number is::FFFFFFFF

No comments:

Post a Comment