Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:56:09+00:00 2026-05-26T03:56:09+00:00

I managed to build a NASM tutorial code for dealing with files. It outputs

  • 0

I managed to build a NASM tutorial code for dealing with files. It outputs the contents of a file to stdout just fine, but when I try to access the data buffer, it contains just zeros. For example in the code below in middle loop EBX is always set to 0, when it should contain file bytes.

section .data
   bufsize dw      1024

section .bss
   buf     resb    1024


section  .text              ; declaring our .text segment
  global  _start            ; telling where program execution should start

_start:                     ; this is where code starts getting exec'ed

  ; get the filename in ebx
    pop   ebx               ; argc
    pop   ebx               ; argv[0]
    pop   ebx               ; the first real arg, a filename

  ; open the file
    mov   eax,  5           ; open(
    mov   ecx,  0           ;   read-only mode
    int   80h               ; );

  ; read the file
    mov     eax,  3         ; read(
    mov     ebx,  eax       ;   file_descriptor,
    mov     ecx,  buf       ;   *buf,
    mov     edx,  bufsize   ;   *bufsize
    int     80h             ; );

    mov ecx, 20
loop:
    mov eax, 20
    sub eax, ecx
    mov ebx, [buf+eax*4]
    loop loop       

  ; write to STDOUT
    mov     eax,  4         ; write(
    mov     ebx,  1         ;   STDOUT,
    mov     ecx,  buf       ;   *buf
    int     80h             ; );

  ; exit
    mov   eax,  1           ; exit(
    mov   ebx,  0           ;   0
    int   80h               ; );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:56:09+00:00Added an answer on May 26, 2026 at 3:56 am

    For example in the code below in middle loop EBX is always set to 0, when it should contain file bytes.

    How are you determining this? (Running under a debugger, perhaps?)

    Your code has an unfortunate bug:

     ; read the file
        mov     eax,  3         ; read(
        mov     ebx,  eax       ;   file_descriptor,
    

    You’re overwriting EAX (which contains the file descriptor returned by the open syscall, if the open succeeded) with the value 3, before it gets moved to EBX as the file descriptor argument to read.

    Normally, a process will start out with file descriptors 0, 1 and 2 assigned to stdin, stdout and stderr, and the first file descriptor that you explicitly open will be 3, so you’ll get away with it!

    But you might not be so lucky if you’re running with a debugger. File descriptor 3 might be something else, and the read might fail (you don’t check to see if the returned value is a negative error code), or read something completely unexpected…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can build a managed custom action just fine using the DTF tools (I.E
Can .NET (managed code) read and write to CouchDB? I would like to build
I'm new to building addins for Visual Studio, but have managed to build a
I'm new to ASP.NET, C# and the MVC framework but have managed to build
So I have just managed to build a timer on my iOS app I'm
I managed to build my storyboard behind code. I don't know how to add
In CDT's managed builder, how are individual files assigned to build configurations? Specifically, how
Has anyone managed to build Erlang on OpenSolaris? Every time I try I get:
I have just managed to build my first C extension for Python, using Cython
I'm new to programming but have managed to build a rather complex dynamic site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.