[assembly] Writing a simple i/o program

In summary, the conversation discusses how to write a program in NASM to read in a character and output what type of character it is. The code includes segments for data and text, as well as a main function and various labels for different types of characters. The conversation also talks about using equates to generate a symbol for the length of a string and the purpose of the "10" in the code. Finally, the conversation mentions that the code is being debugged on a Linux operating system and explains the need for the "10" in the code.
  • #1
twoski
181
2

Homework Statement



I have to write a simple program in NASM to read in a character then output what kind of character it is.


The Attempt at a Solution



There are some things about NASM that confuse me so i marked them.

Code:
segment .data

	BUFFLEN equ 1 ; length of buffer
	CtrlMsg db "Control Character", 10 ;what does this 10 do?
	SymbolMsg db "Symbol", 10
	NumberMsg db "Number", 10
	CapitalMsg db "Capital Letter", 10
	LowercaseMsg db "Lower Case Letter", 10

segment .bss

	input resb 1 ; characters are 1 byte

segment .text

	global _start 

_start: ; main function

	mov eax, 3
    mov ebx, 1
	mov ecx, input
    mov edx, BUFFLEN
	int 0x80 ; interrupt and get user input
	
	cmp byte [ecx],20h ; go through the ascii table ranges 
	jb IsControl
	cmp byte [ecx],30h
	jb IsSymbol
	cmp byte [ecx],3Ah
	jb IsNumber
	cmp byte [ecx],41h
	jb IsSymbol
	cmp byte [ecx],5Bh
	jb IsCapital
	cmp byte [ecx],61h
	jb IsSymbol
	cmp byte [ecx],7Bh
	jb IsLowercase
	cmp byte [ecx],7Fh
	je IsControl 
	
	jmp IsSymbol ; for anything else, assume it's a symbol
	
IsLowercase:

	mov	edx, len	; how do you get the length of a string in nasm?    
	mov	ecx, LowercaseMsg 
	jmp Print	

IsCapital:

	mov	edx, len		    
	mov	ecx, CapitalMsg 
	jmp Print
	
IsSymbol:

	mov	edx, len		    
	mov	ecx, SymbolMsg 
	jmp Print
	
IsNumber:

	mov	edx, len		  
	mov	ecx, NumberMsg   
	jmp Print
	
IsControl:

	mov	edx, len		 
	mov	ecx, CtrlMsg
	jmp Print
	
Print:

	mov	ebx, 1		; writing to screen
	mov	eax, 4		; sysout command
	int	0x80		; interrupt
	
exit: 

	mov eax, 1 ; Select system call 1
	xor ebx, ebx ; Set default exit code
	int 0x80 ; Invoke selected system call
 
Physics news on Phys.org
  • #2
What type of operating system is this running on? The '10' on the strings is an ASCII line feed.

If you want to generate a symbol for the length of a string, you can use an equate, in MASM, where $ means the current location, this would be:

Code:
CtrlMsg    db     "Control Character", 10
CtrlMsgLen equ    $ - offset CtrlMsg
 
  • #3
rcgldr said:
What type of operating system is this running on? The '10' on the strings is an ASCII line feed.

If you want to generate a symbol for the length of a string, you can use an equate, in MASM, where $ means the current location, this would be:

Code:
CtrlMsg    db     "Control Character", 10
CtrlMsgLen equ    $ - offset CtrlMsg

I'm doing all my debugging etc on Linux.

Is the '10' needed at all or is it just there to make the output pretty?

Code:
	CtrlMsg db "Control Character", 10
	CtrlMsgLen equ $ - CtrlMsg
	SymbolMsg db "Symbol", 10
	SymbolMsgLen equ $ - SymbolMsg
	NumberMsg db "Number", 10
	NumberMsgLen equ $ - NumberMsg
	CapitalMsg db "Capital Letter", 10
	CapitalMsgLen equ $ - CapitalMsg
	LowercaseMsg db "Lower Case Letter", 10
	LowercaseMsgLen equ $ - LowercaseMsg
 
  • #4
twoski said:
Is the '10' needed at all or is it just there to make the output pretty?
If you output multiple messages, they'll all end up connected as one message on the screen if you don't have the '10'. For Windows / Dos, you need a carriage return and a line feed (13, 10). Usually control characters are written using hex notation, so line feed would be 00ah.
 
  • #5


First of all, great job on taking on the task of writing a simple I/O program in NASM. It can be a bit confusing at first, but with practice and understanding of the syntax, you'll get the hang of it.

To answer your question about the "10" in the messages, that is used as a newline character. So when the message is printed, it will end with a new line.

As for getting the length of a string in NASM, you can use the "len" label that is defined in the .bss section. This is a common practice in NASM to define labels for data or strings that will be used in the program.

Overall, your code looks good and you have successfully implemented the logic to determine the type of character inputted. Keep practicing and experimenting with NASM, and you will become more comfortable with it.
 

FAQ: [assembly] Writing a simple i/o program

What is assembly language?

Assembly language is a low-level programming language that is used to directly control a computer's hardware. It is made up of mnemonic codes and symbols that represent machine instructions, making it easier for humans to understand and write code for a computer to execute.

How is assembly language different from other programming languages?

Unlike high-level programming languages, which are more human-readable and portable, assembly language is specific to a particular type of computer architecture. It also requires a deep understanding of the computer's hardware and instruction set.

What is the purpose of writing a simple i/o program in assembly?

Writing a simple i/o program in assembly allows for direct control and manipulation of input and output devices, such as keyboards and monitors. It is also useful for low-level tasks where speed and efficiency are crucial.

How do you write a simple i/o program in assembly?

To write a simple i/o program in assembly, you will need to use the appropriate input and output instructions for your computer's architecture. This may include instructions for reading and writing data, as well as interrupt routines to handle input and output operations.

What are some potential challenges when writing a simple i/o program in assembly?

Some potential challenges when writing a simple i/o program in assembly include the need for a deep understanding of the computer's hardware and instruction set, as well as the potential for errors due to the low-level nature of the language. Debugging can also be more difficult in assembly compared to higher-level languages.

Similar threads

Replies
7
Views
7K
Replies
4
Views
2K
Replies
4
Views
4K
Replies
4
Views
6K
Replies
11
Views
4K
Back
Top