[SOLVED] [asm question] How come this prints the version strings the way it does.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[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
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:
how can I define something the length of the variable version:
Quote:
Originally Posted by errigour
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.
That general pattern is the first thing declared after xxx is yyy equ $-xxx
Quote:
Originally Posted by errigour
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.