Nasm: Problem in print Hex info from BMP image

37 Views Asked by At

My goal with this code is, like Hexdump, print the hexadecimal information of the BMP image given as an argument by the terminal. The problem is that whenever I run the program I receive BM_ instead of the image's hexadecimal information. From my experience with the functions, I believe that the problem is found in the openAndReadBMPFile function or in the code before calling the printStr function and after calling the openAndReadBMPFile function.

section .data

    LF equ 10 
    NULL equ 0 
    TRUE equ 1
    FALSE equ 0
    EXIT_SUCCESS equ 0 
    STDIN equ 0 
    STDOUT equ 1 
    STDERR equ 2 
    SYS_read equ 0 
    SYS_write equ 1 
    SYS_open equ 2 
    SYS_close equ 3 
    SYS_fork equ 57 
    SYS_exit equ 60 
    SYS_creat equ 85 
    SYS_time equ 201 
    O_CREAT equ 0x40
    O_TRUNC equ 0x200
    O_APPEND equ 0x400
    O_RDONLY equ 000000q 
    O_WRONLY equ 000001q 
    O_RDWR equ 000002q 
    S_IRUSR equ 00400q
    S_IWUSR equ 00200q
    S_IXUSR equ 00100q

    BUFF_SIZE equ 255 
    newLine db LF, NULL 

    bmpFileDesc         dq  0
    sgFileDesc         dq  0
    MAX_IMG_SIZE        equ 1048576

    errMsgOpenBmp       db "Erro ao abrir o BMP", LF, NULL
    errImgRead          db "Erro ao ler o BMP", LF, NULL


section .bss

    readImgBuffer: resb MAX_IMG_SIZE 


section .text


printStr:
    push rbp
    mov rbp, rsp
    push rbx
    mov rbx, rdi
    mov rdx, 0   
    
strCountLoop:
    cmp byte [rbx], NULL
    je strCountDone
    inc rdx
    inc rbx
    jmp strCountLoop   
    
strCountDone:
    cmp rdx, 0
    je prtDone
    mov rax, SYS_write
    mov rsi, rdi
    mov rdi, STDOUT 
    syscall     
    
prtDone:
    pop rbx
    pop rbp
    ret
        
printStrLn:
    call printStr
    mov rdi, newLine
    call printStr
    ret


errorOnOpenBmp:
    mov rdi, errMsgOpenBmp
    call printStr
    call terminate
    
errorOnRead:
    mov rdi, errMsgRead
    call printStr
    call terminate


openAndReadBMPFile: 

    push rsi 
    mov rax, SYS_open 
    mov rsi, O_RDONLY 
    syscall 
    cmp rax, 0
    jl errorOnOpenBmp
    mov [bmpFileDesc], rax 
    mov rax, SYS_read

    mov rdi, [bmpFileDesc]
    pop rsi 
    mov rsi, readImgBuffer
    mov rdx, MAX_IMG_SIZE

    syscall
    cmp rax, 0
    jl errorOnRead
    push rax 
    mov rax, SYS_close
    mov rdi, qword [bmpFileDesc]
    syscall
    pop rax
    ret



global _start 
_start: 

mov rdi, [rsp + 16]
call openAndReadBMPFile

mov rsi, readImgBuffer
mov byte [rsi+rax], NULL
mov rdi, readImgBuffer
call printStr

terminate:
    mov rax, SYS_exit
    mov rdi, EXIT_SUCCESS
    syscall
0

There are 0 best solutions below