LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-02-2013, 07:29 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Question assigning numbers to registers for nasm


How do I assign the number 0x31 to the eax register. the code below doesn't seem to do the job for sys_write. Is it possable to do that without defining
a variable?

Code:
mov ecx,31h

Last edited by errigour; 01-02-2013 at 07:40 PM.
 
Old 01-04-2013, 05:33 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
try this:
Code:
movl $0x31,%ecx
 
Old 01-04-2013, 03:48 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
I forgot to mention I was using nasm syntax is it possable?
 
Old 01-05-2013, 02:02 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The statement you wrote was already in nasm syntax. Did you get any error message? Or did you use the debugger the find out anything?

(PS: the word you were trying to use is 'possible')
 
Old 01-05-2013, 04:31 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
no it doesn't put the number in ecx when I do a syste_write it prints nothing when it should be printing 1 to the screen
 
Old 01-05-2013, 04:35 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by errigour View Post
no it doesn't put the number in ecx when I do a syste_write it prints nothing when it should be printing 1 to the screen
I think you have misunderstood the write function you are calling.

Try posting more of your code and maybe someone can explain more of what you did wrong.

The instruction you quoted does put 31h into ecx.
 
Old 01-05-2013, 05:45 PM   #7
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Code:
     mov edx,1          ;Amount of data to right
     mov ecx,0x31       ;Data to write
     mov ebx,1          ;file descriptor (stdout)
     mov eax,4          ;system call number (sys_write)
     int 80h
 
Old 01-05-2013, 06:22 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
ecx is supposed to be the address of the data to write, not the actual data.
 
1 members found this post helpful.
Old 01-05-2013, 10:23 PM   #9
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
alright that helped thanks
 
Old 01-06-2013, 12:10 AM   #10
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Hey I understand but I can't figure out how to make this thing do what I want. could one of you guys maybe give me an example?

line 91
line 152

Are the lines that I want numbers to be printed but they just aren't printing anything.

Code:
; ..  .$$.  $$$$$$    .$$ ..        .$$.      $$    .$    .$$   .$$..$$.   $$.   $       .$$   .$$.   
; $$ $$$$$  $$$$$$  .$$$$ $$       .$$$$$    $$$   .$$   $$$$  $$$$$$$$$  $$$$$  $     .$$$$  $$$$$   
; $$ $"  $$   $$    $$"   $$       $$  "$.   $     $$   .$"    $"  $$  $$ $" "$  $     $$"   $$"  $   
; $$ $   $$   $$   .$     $$       $    $$   $     $$   $"     $   $$  $$ $  .$  $    .$     $$    $  
; $$ $   $$   $$   $$$$$$ $$    .  $    $$   $     $$   $$$$$  $   $$  $$ $.$$$  $    $$$$$$ $$   .$  
; $$ $   $$   $$   $$$$$$ $$    $$ $  $$$$   $     $$   $$$$$  $   $$  $$ $"$$$  $    $$$$$$ $$ ..$   
; $$ $   $$   $$   "$     $$    "  $  $$$$   $     $$   $.     $   $$  $$ $  "$  $    "$     $$ $$    
; $$ $   $$   $$    $$.   $$       $    $$  .$     $$   "$.    $   $$  $$ $   $  $.    $$.   $$ $.    
; $$ $   $$   $$    "$$$$  $$$     $    $$ $$$   .$$"    $$$$  $   $$  $$ $.$$$  $$$$  "$$$$ $$ "$$   
; "" $   $$   ""      "$$  "$$     $    $$ $$    "$"      "$$  $   ""  $$ $"$$    "$$    "$$ ""  "$   
;         $$                             $$                             $$                            
;         "$..                            $..                           "$..                          
;          "$$                            ""$$                            "$$                         

SECTION .data
     dupped: TIMES 10 db 31h
     prmpt     db    '# '               ; Command prompt

     LF        db    0x0A               ; Line feed
     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,"$     $$ $$   ",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
     sbyte:    resb 4
     stdin:    resb 10000
     lenin     equ  $-stdin              ; Length of input to receive for stdin


SECTION	.text     ; Assembly code
     global _start  ;must be declared for linker (ld)
    
_start:             ;tell linker entry point
	mov eax,4
	mov ebx,1
     mov ecx,version
     mov edx,100
	int 80h
	mov ecx,version_
	mov eax,4
	int 80h
	mov ecx,version__
	mov eax,4
	int 80h
	mov ecx,version___
	mov eax,4
	int 80h
	mov ecx,version____
	mov eax,4
	int 80h
	mov ecx,version_____
	mov eax,4
	int 80h
	mov ecx,version______
	mov eax,4
	int 80h
	mov ecx,version_______
	mov eax,4
	int 80h
	mov ecx,version________
	mov eax,4
	int 80h
	mov ecx,version_________
	mov eax,4
	int 80h
	mov ecx,version__________
	mov eax,4
	int 80h
	mov ecx,version___________
	mov eax,4
	int 80h
	mov ecx,version____________
	mov eax,4
	int 80h

     ; Print the number of arguments
     mov edx,100
     pop ecx            ; Get the number of arguments
     mov byte [sbyte], cl
     mov ecx, sbyte
     mov eax,4
     int 80h

     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h
     
     ; Print the program name
     mov edx,100
     pop ecx            ; Get the program name
     mov eax,4
     int 80h

     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h
     
     ;Print first argument
     mov edx,100
     pop ecx            ; Get the first actual argument
     mov eax,4
     int 80h

     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h
     
     ;Print second argument
     mov edx,100
     pop ecx            ; Get the second actual argument
     mov eax,4
     int 80h

     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h
     
     ;Print third argument
     mov edx,100
     pop ecx            ; Get the third actual argument
     mov eax,4
     int 80h

     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h

     ; Print the number 1
     mov eax,4          ;system call number (sys_write)
     mov byte [sbyte], 31h
     mov ecx, sbyte
     mov edx,10         ;Amount of data to right
     int 80h
     
     ; Print new line
     mov eax,4          ;system call number (sys_write)
     mov ecx,LF         ;Data to write
     mov edx,1          ;Amount of data to right
     int 80h
     
_recvstdin:
     ;print command 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
_recv:
     ;receive input
     mov edx,100        ;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	_cmdquit
	cmp	ecx,5
	je	_exit
	jmp	_cmdexit_
	
_cmdquit:
	mov	ebx,CMDQUIT
	mov	ecx,0
	mov	edx,stdin
_cmdquit_:
	inc	ecx
	mov	ah,[edx]
	mov	al,[ebx]
	inc	edx
	inc	ebx
	cmp	ah,al
	jne	_cmdecho
	cmp	ecx,5
	je	_exit
	jmp	_cmdquit_

_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 01-06-2013, 06:44 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by errigour View Post
Code:
     ; Print the number of arguments
     mov edx,100
     pop ecx            ; Get the number of arguments
     mov byte [sbyte], cl
You are treating the number of arguments as an ascii code. But it isn't. You need to convert it to ascii. If you know it will be just one digit, then converting to ascii is just adding 30h to it. If it might be more than one digit, converting it to ascii requires first breaking it up into decimal digits.

Also, why do you set edx to 100? Aren't you trying to print just one digit?

Also, why do you think you get the number of arguments with that pop instruction? I'm not sure what is where when asm code starts in Linux, but I didn't expect the stack layout you seem to expect.

Last edited by johnsfine; 01-06-2013 at 06:47 AM.
 
Old 01-06-2013, 09:36 PM   #12
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
why doesn't this print a number and how can I make it print the number 1?
Code:
     ; Print the number 1
     mov eax,1          ;system call number (sys_write)
     mov byte [sbyte], 31h
     mov ecx, [sbyte]
     mov edx,10         ;Amount of data to right
     int 80h
 
Old 01-07-2013, 07:17 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by errigour View Post
why doesn't this print a number and how can I make it print the number 1?
Code:
     ; Print the number 1
     mov eax,1          ;system call number (sys_write)
     mov byte [sbyte], 31h
     mov ecx, [sbyte]
     mov edx,10         ;Amount of data to right
     int 80h
In nasm syntax the characters I marked in red [ ] tell it you want the contents of the memory location. But you need ecx to hold the address, not the contents. So try it without the [ ].

But on the line above that, the [ ] are correct, because you want the 31h to be stored as the contents of that location.
 
Old 01-07-2013, 02:10 PM   #14
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
One minor point, just to satisfy my inner pedant, and because I think if you're going to be an assembler programmer you might as well talk like one.
In assembler, we generally don't use the term 'assign' in this context. It is not the same as assigning a value to a variable in a high level language. You are specifying/writing a specific machine instruction, and that instruction is to load an immediate value into a specified register. The immediate data is part of the code and will appear as the byte(s) immediately following the opcode byte(s) in the binary object code.
I know it is a bit of a nit, but nits left unpicked can grow into full-on bugs.

--- rod.
 
Old 01-08-2013, 06:13 PM   #15
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
dang thing is still not printing a number
 
  


Reply



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
nasm assembler; output numbers? infinity42 Programming 4 05-26-2018 01:22 PM
assigning color to numbers and plotting in GNUPLOT pravas Linux - Newbie 1 12-08-2011 12:05 PM
Assigning large numbers of IPv6 addresses jack_sprat Linux - Networking 0 10-02-2007 10:04 AM
Assigning IP numbers on home network moonmoth Linux - Newbie 3 11-18-2004 11:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration