[assembly] Writing a simple i/o program

AI Thread Summary
The discussion focuses on writing a simple NASM program that reads a character and identifies its type, such as control character, symbol, number, capital letter, or lowercase letter. Participants clarify that the '10' in the string definitions represents an ASCII line feed, which is necessary for formatting output, especially when multiple messages are displayed. The program structure includes segments for data, BSS, and text, with specific instructions for reading input and comparing ASCII values. There is also a mention of debugging on Linux and the importance of control characters for proper output formatting. Overall, understanding NASM syntax and proper output handling is emphasized in the context of the assignment.
twoski
Messages
177
Reaction score
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
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
 
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
 
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.
 
Back
Top