I’m trying to make a small x86 program that gets as input the absolute path of a folder, which contains .txt files, and prints their size and fd

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.data
formatString: .asciz "%s"
descLabel: .asciz "File Descriptor: %dn"
sizeLabel: .asciz "File Size: %d kilobytesnn"
afis_nr: .asciz "%d n"
# Buffers and variables
path: .space 256
dirPtr: .space 4 # pointer to DIR structure
dirEntryPtr: .space 4 # pointer to dirent structure
.text
CONCRETE:
pushl %ebp
movl %esp, %ebp
leal path, %edi # address of the path buffer
pushl %edi
call opendir
addl $4, %esp
movl %eax, dirPtr
read_dir:
movl dirPtr, %edi
pushl %edi
call readdir
addl $4, %esp
testl %eax, %eax # check if null
je close_dir
movl %eax, dirEntryPtr # save dirent* pointer
# get the d_name field of dirent
movl dirEntryPtr, %edi
addl $11, %edi # address of d_name in dirent
pushl %edi # open the file using d_name
call open
addl $4, %esp
movl %eax, %ebx # save file descriptor
pushl %ebx # print file descriptor
pushl $descLabel
call printf
addl $8, %esp
# get file size with lseek
movl $8, %eax
movl %ebx, %edi # file descriptor
xorl %esi, %esi # offset
movl $2, %edx # SEEK_END
int $0x80
movl %eax, %ecx # save file size
addl $1023, %ecx # calculate file size in kilobytes
shrl $10, %ecx # divide by 1024
pushl %ecx # print file size
pushl $sizeLabel
call printf
addl $8, %esp
movl $3, %eax # syscall: close
movl %ebx, %edi
int $0x80
jmp read_dir
close_dir:
movl dirPtr, %edi
pushl %edi
call closedir
addl $4, %esp
exit_CONCRETE:
popl %ebp
ret
.global main
main:
pushl $path
pushl $formatString
call scanf
addl $8, %esp
call CONCRETE
movl $1, %eax
xorl %ebx, %ebx
int $0x80
</code>
<code>.data formatString: .asciz "%s" descLabel: .asciz "File Descriptor: %dn" sizeLabel: .asciz "File Size: %d kilobytesnn" afis_nr: .asciz "%d n" # Buffers and variables path: .space 256 dirPtr: .space 4 # pointer to DIR structure dirEntryPtr: .space 4 # pointer to dirent structure .text CONCRETE: pushl %ebp movl %esp, %ebp leal path, %edi # address of the path buffer pushl %edi call opendir addl $4, %esp movl %eax, dirPtr read_dir: movl dirPtr, %edi pushl %edi call readdir addl $4, %esp testl %eax, %eax # check if null je close_dir movl %eax, dirEntryPtr # save dirent* pointer # get the d_name field of dirent movl dirEntryPtr, %edi addl $11, %edi # address of d_name in dirent pushl %edi # open the file using d_name call open addl $4, %esp movl %eax, %ebx # save file descriptor pushl %ebx # print file descriptor pushl $descLabel call printf addl $8, %esp # get file size with lseek movl $8, %eax movl %ebx, %edi # file descriptor xorl %esi, %esi # offset movl $2, %edx # SEEK_END int $0x80 movl %eax, %ecx # save file size addl $1023, %ecx # calculate file size in kilobytes shrl $10, %ecx # divide by 1024 pushl %ecx # print file size pushl $sizeLabel call printf addl $8, %esp movl $3, %eax # syscall: close movl %ebx, %edi int $0x80 jmp read_dir close_dir: movl dirPtr, %edi pushl %edi call closedir addl $4, %esp exit_CONCRETE: popl %ebp ret .global main main: pushl $path pushl $formatString call scanf addl $8, %esp call CONCRETE movl $1, %eax xorl %ebx, %ebx int $0x80 </code>
.data
    formatString: .asciz "%s"
    descLabel: .asciz "File Descriptor: %dn"
    sizeLabel: .asciz "File Size: %d kilobytesnn"
    afis_nr: .asciz "%d n"

    # Buffers and variables
    path: .space 256
    dirPtr: .space 4 # pointer to DIR structure
    dirEntryPtr: .space 4 # pointer to dirent structure

.text
CONCRETE:
    pushl %ebp
    movl %esp, %ebp

    leal path, %edi # address of the path buffer
    pushl %edi
    call opendir
    addl $4, %esp
    movl %eax, dirPtr

read_dir:
    movl dirPtr, %edi
    pushl %edi
    call readdir
    addl $4, %esp
    testl %eax, %eax # check if null
    je close_dir
    movl %eax, dirEntryPtr # save dirent* pointer

    # get the d_name field of dirent
    movl dirEntryPtr, %edi
    addl $11, %edi # address of d_name in dirent

    pushl %edi # open the file using d_name
    call open
    addl $4, %esp
    movl %eax, %ebx # save file descriptor


    pushl %ebx # print file descriptor
    pushl $descLabel
    call printf
    addl $8, %esp

    # get file size with lseek
    movl $8, %eax
    movl %ebx, %edi # file descriptor
    xorl %esi, %esi # offset
    movl $2, %edx # SEEK_END
    int $0x80
    movl %eax, %ecx # save file size

    addl $1023, %ecx # calculate file size in kilobytes
    shrl $10, %ecx # divide by 1024

    pushl %ecx # print file size
    pushl $sizeLabel
    call printf
    addl $8, %esp

    movl $3, %eax # syscall: close
    movl %ebx, %edi
    int $0x80

    jmp read_dir

close_dir:
    movl dirPtr, %edi
    pushl %edi
    call closedir
    addl $4, %esp

exit_CONCRETE:
    popl %ebp
    ret

.global main
main:
    pushl $path
    pushl $formatString
    call scanf
    addl $8, %esp

    call CONCRETE

    movl $1, %eax
    xorl %ebx, %ebx
    int $0x80

I want to have the code print the file descriptor and size (in kilobytes), but for a folder that is given as “input” with 16 .txt files, I get 18 outputs, 16 of them having the descriptor return with -1, and the size not being calculated properly; I’m very new to assembly languages, so I suppose there must be something wrong with the offset?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # get the d_name field of dirent
movl dirEntryPtr, %edi
addl $11, %edi # address of d_name in dirent
</code>
<code> # get the d_name field of dirent movl dirEntryPtr, %edi addl $11, %edi # address of d_name in dirent </code>
    # get the d_name field of dirent
    movl dirEntryPtr, %edi
    addl $11, %edi # address of d_name in dirent

maybe something wrong here?

I see at least three problems:

  1. The paths you get from readdir are relative to the directory you called readdir on, but the paths you pass to open need to be relative to the current directory.
  2. You’re using the syscall numbers from x86_64 even though you’re targeting 32-bit x86.
  3. Your code runs on both files and directories, but lseek on a directory isn’t a valid way of getting its size.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật