LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-30-2012, 11:31 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 271

Rep: Reputation: 5
[asm question] How come this prints the version strings the way it does.


This should compile fine:
nasm -f elf -o asm1.o asm1
ld -o asm1 asm1.o

thing is it doesn't do what i want it to it actually prints the entire data section exceeding the variable each time sys_write is called. is nasm misbehaving?

Code:
SECTION .data
spr:	  db    "Special Purpose Registers",10,0
prmpt:    db	'# '			; Command prompt, two bytes in succession

CMDEXIT   db	"exit", 0x0A     	; Command exit,causes jmp _exit
CMDQUIT   db	"quit", 0x0A		; Command quit causes jmp _exit
CMDECHO   db    "echo", 0x0A		; Command echo causes jmp _echo

version 		db	"..  .$$.  $$$$$$    .$$ ..        .$$.      $$    .$    .$$   .$$..$$.   $$.   $       .$$   .$$.",0x0A
version_ 		db      "$$ $$$$$  $$$$$$  .$$$$ $$       .$$$$$    $$$   .$$   $$$$  $$$$$$$$$  $$$$$  $     .$$$$  $$$$$",0x0A
version__ 		db	"$$ $",0x22,"  $$   $$    $$",0x22,"   $$       $$  ",0x22,"$.   $     $$   .$",0x22,"    $",0x22,"  $$  $$ $",0x22," ",0x22,"$  $     $$",0x22,"   $$",0x22,"  $",0x0A
version___ 		db	"$$ $   $$   $$   .$     $$       $    $$   $     $$   $",0x22,"     $   $$  $$ $  .$  $    .$     $$    $",0x0A
version____ 		db	"$$ $   $$   $$   $$$$$$ $$    .  $    $$   $     $$   $$$$$  $   $$  $$ $.$$$  $    $$$$$$ $$   .$",0x0A
version_____ 		db	"$$ $   $$   $$   $$$$$$ $$    $$ $  $$$$   $     $$   $$$$$  $   $$  $$ $",0x22,"$$$  $    $$$$$$ $$ ..$",0x0A
version______ 		db	"$$ $   $$   $$   ",0x22,"$     $$    ",0x22,"  $  $$$$   $     $$   $.     $   $$  $$ $  ",0x22,"$  $    ",0x22,"$     $$ $$",0x22,0x0A
version_______  	db	"$$ $   $$   $$    $$.   $$       $    $$  .$     $$   ",0x22,"$.    $   $$  $$ $   $  $.    $$.   $$ $.",0x0A
version________ 	db	"$$ $   $$   $$    ",0x22,"$$$$  $$$     $    $$ $$$   .$$",0x22,"    $$$$  $   $$  $$ $.$$$  $$$$  ",0x22,"$$$$ $$ ",0x22,"$$",0x0A
version_________ 	db	"",0x22,"",0x22," $   $$   ",0x22,"",0x22,"      ",0x22,"$$  ",0x22,"$$     $    $$ $$    ",0x22,"$",0x22,"      ",0x22,"$$  $   ",0x22,"",0x22,"  $$ $",0x22,"$$    ",0x22,"$$    ",0x22,"$$ ",0x22,"",0x22,"  ",0x22,"$",0x0A
version__________ 	db	"        $$                             $$                             $$",0x0A
version___________ 	db	"        ",0x22,"$..                            $..                           ",0x22,"$..",0x0A
version____________ 	db	"         ",0x22,"$$                            ",0x22,"",0x22,"$$                            ",0x22,"$$",0x0A

SECTION .bss  ; Uninitialized data
stdin:	resb	64			 ; 64 bytes for stdin
lenin	equ	$-stdin			 ; Length of input to receive for stdin
lenver: resb    7

SECTION	.text ; Assembly code
	global _start			;must be declared for linker (ld)
    
_start:			;tell linker entry point

	mov	edx,lenver
	mov	ecx,version
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version_
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version__
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version___
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version____
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version_____
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version______
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version_______
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version________
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version_________
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version__________
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version___________
	mov	ebx,1
	mov	eax,4
	int	80h
	mov	edx,lenver
	mov	ecx,version____________
	mov	ebx,1
	mov	eax,4
	int	80h

_recvstdin:
	;print comman line prompt
	mov	edx,2		;Amount of data to right
	mov	ecx,prmpt       ;Data to write
	mov	ebx,1		;file descriptor (stdout)
	mov	eax,4		;system call number (sys_write)
	int	80h

	;receive input
	mov	edx,lenin	;message length
	mov	ecx,stdin	;message to read
	mov	ebx,2		;file descriptor (stdin)
	mov	eax,3		;system call number (sys_read)
	int	80h
	
_cmdexit:
	mov	ebx,CMDEXIT
	mov	ecx,0
	mov	edx,stdin
_cmdexit_:
	inc	ecx
	mov	ah,[edx]
	mov	al,[ebx]
	inc	edx
	inc	ebx
	cmp	ah,al
	jne	_cmdecho
	cmp	ecx,5
	je	_exit
	jmp	_cmdexit_

_cmdecho:
	mov	ebx,CMDECHO
	mov	ecx,0
	mov	edx,stdin
_cmdecho_:
	inc	ecx
	mov	ah,[edx]
	mov	al,[ebx]
	inc	edx
	inc	ebx
	cmp	ah,al
	jne	_recvstdin
	cmp	ecx,4
	je	_echo
	jmp	_cmdecho_

_echo:
	mov	ecx, 0
	mov	edx, stdin
_parse:
	mov	ah, [edx]
	cmp	ah, 0x0A
	je	_echo_
	inc 	ecx
	inc	edx
	jmp	_parse
_echo_:
	;echo input
	inc 	ecx
	mov	edx,ecx  	;message length
	mov	ecx,stdin	;message to write
	mov	ebx,1		;file descriptor (stdin)
	mov	eax,4		;system call number (sys_write)
	int	80h
	jmp	_recvstdin

_echo_usage:
	jmp	_recvstdin

_exit:	
	
	mov	ebx,0
	mov	eax,1		;system call number (sys_exit)
	int	0x80
 
Old 09-30-2012, 12:07 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,087

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
What did you intend to accomplish with the following code?

Quote:
Originally Posted by errigour View Post
Code:
lenver: resb    7
...
     mov    edx,lenver
If you expected that to put 7 in edx, you made at least two mistakes.
 
Old 09-30-2012, 08:47 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 271

Original Poster
Rep: Reputation: 5
oh ok thanks. how can I define something the length of the variable version:
or do I gotta count each character of variable until it equals 0x0A.
or use a function I basically i was asking if I can define a variable the length of variable:

Last edited by errigour; 09-30-2012 at 09:04 PM.
 
Old 10-01-2012, 07:59 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,087

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by errigour View Post
how can I define something the length of the variable version:
Quote:
Originally Posted by errigour View Post
Code:
stdin:    resb    64             ; 64 bytes for stdin
lenin    equ    $-stdin             ; Length of input to receive for stdin
The code you already have (I assume copied from somewhere) already includes an example of computing the size of a memory area. It is a trivial example, because you can directly see the size is 64. But whoever originally wrote that computed the size in the general way, either as an example or to make sure any later change in the code keeps the actual size and the computed size in sync.

That general pattern is the first thing declared after xxx is
yyy equ $-xxx
so that yyy becomes the number of bytes occupied by xxx.

Last edited by johnsfine; 10-01-2012 at 08:02 AM.
 
Old 10-01-2012, 11:55 AM   #5
errigour
Member
 
Registered: May 2009
Posts: 271

Original Poster
Rep: Reputation: 5
that doesn't work with nasm that is the .bss section it wont let me create $-version
 
Old 10-01-2012, 01:46 PM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,087

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by johnsfine View Post
That general pattern is the first thing declared after xxx is
yyy equ $-xxx

Quote:
Originally Posted by errigour View Post
that doesn't work with nasm that is the .bss section it wont let me create $-version
Please notice what I said above. The meaning of $ is specific to where the line of code containing the $ is located.

So if that line is not in the right place, the meaning will be wrong. Since you put the line in a different section, that wrong meaning asks for a subtraction $-version that nasm cannot compute.

Last edited by johnsfine; 10-01-2012 at 01:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inline asm, 64-bit asm, intercepting segfaults, etc. rainbowsally Programming 0 02-04-2012 05:10 PM
IA64 ASM Question SaraiKhan Programming 29 12-18-2008 01:29 AM
asm Question Whiteghost Programming 2 09-03-2005 09:26 AM
asm question karlan Programming 6 07-16-2004 11:54 AM
ASM question zWaR Programming 2 06-26-2004 11:42 AM


All times are GMT -5. The time now is 08:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration