简体   繁体   中英

linux nasm code displays nothing

I am making a program where the user enters a number, and it prints out all the numbers from zero up to the number. It compiles fine, links fine, and returns no errors when it runs, and yet it prints out absolutely nothing. Here is the code:

SECTION .data 
len   EQU 32

SECTION .bss 
other resd len
data  resd len

SECTION .text

GLOBAL _start
_start:
nop

input:                  ; This section gets the integer from the user
mov eax, 3          ; }
mov ebx, 1          ; }
mov ecx, data       ; } System_read call
mov edx, len        ; }
int 80h             ; }

mov ebp, 1

setup:                  ; This section sets up the registers ready for looping 
mov [other], ebp

loop:                   ; This section loops, printing out from zero to the number given
mov eax, 4
mov ebx, 1
mov ecx, [other]
mov edx, len
int 80h

exit:                   ; Exits the program
mov eax, 1          ; }
mov ebx, 0          ; } System_exit call
int 80h             ; }

When I step through it on KDBG, it returns a few errors; it receives an interrupt and a segmentation fault, although I can't tell where. I'm not sure why though, because when I run it in Geany, it returns a 0 value at the end and runs without error. Why does it not work?

Thanks in advance

NOTE: This code does not loop. It is not finished yet. All it should do here is print out the number 1.

When you go to print, you are calling mov ecx, [other] . This looks at the address that's stored in other and follows that address to get whatever is stored there. The problem is that this system call is expecting an address in ecx, not a value.

If you call mov ecx, other instead, then ecx will have the address of other and it will be able to go to that address and print what's there.

You have another problem here: when you print the number stored in other , it will translate it into the ascii value. So, for example, when you try to print a 1, instead of printing the number 1, it will print ascii 1 (which happens to be a start of heading character; nothing you want to print). Add '0' (the character '0') if you want to print numbers.

EDIT: One more thing, when you read, you are passing 1 into ebx. 1 is STDOUT. What you want is STDIN which is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM