Hello!
I have just started learning assembly language, and I have decided to use the NASM assembler.

I made a small program, included below, that is supposed to take a number, add 5, and print out the final result. The program correctly prints the prompt, and takes a value, but then crashes. NASM makes no warning or error messages.

I would appreciate any help!
-tundra010

The code:

; This program is supposed to get a number, add 5 to it, and print out the final number.

[section .data]
	prompt: db 'Enter a number: ',0
	fmt: db '%d',0

[section .text]
	global _main
	extern _printf
	extern _scanf
	
_main:
	; Display message
	push prompt
	call _printf
	add esp,4
	
	; Get input
	push dword[ecx]
	push fmt
	call _scanf
	add esp,8
	
	; Add 5 to ecx
	mov ecx,5
	
	; Print final value of ecx
	push dword[ecx]
	call _printf
	add esp,4
	
	; End program
	mov eax,0
	ret

Recommended Answers

All 6 Replies

Is this for an assembly language class you are taking? If yes, does your teacher allow you to just call C library functions? That sounds like cheating to me.

line 19: what is the value of ecx?? You need to declare data for scanf() to store the results, just like you would have done in C languge.

No, this is not for a class, I am learning assembly on my own. I have never found a guide that shows how to print data and get input without the C library on windows. If you know how to do this, could you show me?

Also, I declared some data for scanf(), and although the program compiles, It prints out a character instead of a number. Here is my updated code:

[section .data]
	prompt: db 'Enter a number: ',0
	fmt: db '%d',0
	data: db 0

[section .text]
	global _main
	extern _printf
	extern _scanf
	
_main:
	; Display message
	push prompt
	call _printf
	add esp,4
	
	; Get input
	push data
	push fmt
	call _scanf
	add esp,8
	
	; Add 5 to data
	add byte[data],5
	
	; Print final value of data
	push data
	call _printf
	add esp,4
	
	; End program
	mov eax,0
	ret

All the funbctions you need are in int 21h. You can also use keyboard functions in int 16h, which are more flexible. Think of how you would use c language getchar() to get keyboard input and put it into a buffer. The same with assembly, using int 16h functions to get keyboard input.

int 21h has functions to either display a single chacter or a whole string (termiated by $ instead of '\0')

Those links are for dos, right?

I have heard of interrupts on dos, and linux, but never on win32.

I want to program in assembly for the windows x86 platform.

Could you please point me to resources that show how to use interrupts on the win32 platform, for the NASM Assembler?

Thanks!
-tundra010

You are using nasm in MS-DOS mode (command prompt). Yes, they can not be used in win32 32-bit program. If that is what you want to do then interrupts are out of the question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.