* appending \n using registers
@ 2003-11-02 2:12 Jason Roberts
2003-11-02 19:15 ` peter w krause
0 siblings, 1 reply; 5+ messages in thread
From: Jason Roberts @ 2003-11-02 2:12 UTC (permalink / raw)
To: linux-assembly
Hi all---
I have this problem where I want to append a newline to a string.
I implemented a stack version and it works fine-- just push the \n
sequence,push the string
and pass off to kernel write function. Now I want to do the same thing but
through regs--
not working.
Also a concern of mine is the need to specify a 16 bit reg destination
operand for a 2 byte move.
Obviously, using mov eax,src implies a 4 byte move -- is there any way to
'force' a 1 or 2 byte move
into a 32-bit reg. I just want my code to look cleaner and avoid mixing
32-bit and 16-bit regs.
Thanks for any help.
later ,
Paul
section .data
tr db "96"
section .text
global _start
_start:
;output '69' with a \n
;stack method -- works
mov dx,[tr]
xchg dh,dl ; swap
mov edi,0x0a0d00
push edi ;
push dx
mov ecx,esp ;buffer
mov edx,5 ;length of buffer
mov eax,4 ;sys_write
mov ebx,1 ;file descriptor
int 0x80
;register method -- fails
mov dx,[tr]
xchg dh,dl ; swap
mov ecx,edx ;buffer
mov edx,2 ;length of buffer
mov eax,4 ;sys_write
mov ebx,1 ;file descriptor
int 0x80
mov edi,0x0a0d00
mov ecx,edx ;buffer
mov edx,2 ;length of buffer
mov eax,4 ;sys_write
mov ebx,1 ;file descriptor
int 0x80
exit:
mov ebx,eax
mov eax,1
int 0x80
_________________________________________________________________
Concerned that messages may bounce because your Hotmail account has exceeded
its 2MB storage limit? Get Hotmail Extra Storage!
http://join.msn.com/?PAGE=features/es
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: appending \n using registers
2003-11-02 2:12 appending \n using registers Jason Roberts
@ 2003-11-02 19:15 ` peter w krause
2003-11-03 11:21 ` peter w krause
0 siblings, 1 reply; 5+ messages in thread
From: peter w krause @ 2003-11-02 19:15 UTC (permalink / raw)
To: Jason Roberts, linux-assembly
Jason Roberts am Sunday 02 November 2003 02:12:
> Hi all---
> I have this problem where I want to append a newline to a string.
> I implemented a stack version and it works fine-- just push the \n
> sequence,push the string
> and pass off to kernel write function. Now I want to do the same thing but
> through regs--
> not working.
where/what is your buffer? (2nd example)
> Also a concern of mine is the need to specify a 16 bit reg destination
> operand for a 2 byte move.
('as') ('nasm')
movw ax,dest mov ax,[word dest]
> Obviously, using mov eax,src implies a 4 byte move -- is there any way to
> 'force' a 1 or 2 byte move
> into a 32-bit reg. I just want my code to look cleaner and avoid mixing
> 32-bit and 16-bit regs.
-???-
movzwl; movswl movszw, movsw + destn size spec.
copies 16bits, sero or sign extended into 32bit location
hp
--
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml
>> hp -at- lxhp -dot- in-berlin -dot- de <<
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: appending \n using registers
2003-11-02 19:15 ` peter w krause
@ 2003-11-03 11:21 ` peter w krause
0 siblings, 0 replies; 5+ messages in thread
From: peter w krause @ 2003-11-03 11:21 UTC (permalink / raw)
To: Jason Roberts, linux-assembly
peter w krause am Sunday 02 November 2003 19:15:
[...]
>
> ('as') ('nasm')
> movw ax,dest mov ax,[word dest]
^^^^^^^^^^^ ^^^^^^^^^^^^^^^
movw src,ax mov ax,[word src]
[...]
>
> movzwl; movswl movszw, movsw + destn size spec.
^^^^^^
movzwl; movswl movzw, movsw + destn size spec.
hp
--
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml
>> xxxx -at- lxhp -dot- in-berlin -dot- de <<
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: appending \n using registers
[not found] <Law9-F43TlbI3c57jHV0001de3a@hotmail.com>
@ 2003-11-04 13:30 ` peter w krause
0 siblings, 0 replies; 5+ messages in thread
From: peter w krause @ 2003-11-04 13:30 UTC (permalink / raw)
To: Jason Roberts, linux-assembly
Jason Roberts am Monday 03 November 2003 23:12:
> thanks for ideas, albeit they were cryptic.
I find it difficult to explaining in a few words what - by now - seems very
trivial to me:)
> ummm... that lea instruction is weird...
not really (imho, once accustomed).
> nomrally when I think of using brackets I think 'value at this location'
...and LEA yields 'address of 'value at this location''.
> So if eax = 0x8048080 then eax would adress 0x8048080 and [eax] would
> dereference byte at this location, similarly [eax+1] would access byte at
> 0x8048081
> so using lea edx,[eax] seems odd-- looks like it's moving 4 bytes at eax
> ,not the address that eax represents. Am I making sense???
certainly :) but...
besides, I find an operation which yields the result of an address calculation
but doesn't actually store or fetch any data very helpful, for instance while
programming position independent code (shared libs!).
I use LEA to storeing an address into a register and MOV for load/store of
register values or data from/to memory which -imho- helps clarifying the
purpose of asm text. often merely a variant of appearance, though.
other benefits may be that LEA
sometimes compiles shorter and/or simpler code,
can be used for simple + fast arithmetics w/o additional regs,
arithmetics won't modify the CPU flags !
runtime penalty is probably
some extra execution cycles due to 'address generation interlock' if the
immediately following or preceding opr's use the LEA destn register.
I found the LEA w/ 680xx-es, where no (exact) aequivalent to intel style <mov
reg,address> exists, difficult to understand, myself, too.
'LEA' = 'load to destn reg the address of where a mov would load to/from'
>
> also, check out this session with gdb and an objdump of the prog-- you will
> (gdb) ni
> 0x0804813f in ?? ()
> (gdb) i r
> eax 0x2 2
> ecx 0xa0d00 658688 <---------- looks right to me...dont know
> what problem is:(
This is the actual _data_ in ecx but, ECX should hold the buffer ADDRESS!
Always. No exception. Linux provides no system call which sends/receives the
chars directly from/to a register.
Don't get confused by manual pages which, in fact, often refer to the 'libc',
even when stating some routine being a 'system call'. All libc syscalls are
wrappers to the actual kernel system calls.
did you check the returned figure in EAX?
anything in range of 0xfffff000 till 0xffffffff is an error code
successful 'write' returns the number of sent chars, ret EAX := EDX at entry
besides, why did you append the <nul> byte to the <cr><lf> sequence?
best,
hp
> _________________________________________________________________
> Send instant messages to anyone on your contact list with MSN Messenger
which explains...
--
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml
>> hp -at- lxhp -dot- in-berlin -dot- de <<
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: appending \n using registers
[not found] <Law9-F57R88vo2O66rD0001ae77@hotmail.com>
@ 2003-11-03 19:55 ` wklux
0 siblings, 0 replies; 5+ messages in thread
From: wklux @ 2003-11-03 19:55 UTC (permalink / raw)
To: Jason Roberts, bdavids1, linux-assembly
Jason Roberts am Monday 03 November 2003 14:31:
> hehe,
> I gathered what you meant there...but I guess if youre an at&t geek it
I don't like the hybrid att 'syntax', at all, but I prefer AS, because of
compiling speed, reliability, availability...
> would not be trivial!
> now look here again and tell me I'm whacked: I just want to display a 2
> byte string with a \n!
> *note* I do not want to 'embed' a newline.
> Thanks for help
> pb
> section .data
>
> str db "96"
>
> section .text
>
> global _start
>
> jmp _start
>
> _start:
> pusha
> call _test
> popa
> jmp exit
>
>
>
> _test: could this line be culprit? str is 2 bytes but I'm trying to mov 4
> using edx,no?
>
> mov edx,[str] <------- ==> eax := dword at 'str'
this copies the _content_ at 'str' to edx,
i.e. l.s.bytes in memory copied to l.s.ytes in register, thus whether 2 or 4
bytes buffer size won't matter while just reading; exactly matching access
required only to preventing a 'segfault' (linux) when accessing memory just
'below' top bound of reserved space.
I should think that you missed the meaning of the used operation - which seems
a common error when migrating from 'masm'-like assemblers to the linux progs.
nasm expects either,
lea edx,[str]
or,
mov edx,str
to loading an effective address (hence 'LEA').
> mov ecx,edx ; ecx := edx = @str
not necessary. just copies the regs' content.
try 'mov edx,str' or 'lea edx,[str]' instead (I prefer LEA for clarity).
> ----------------------------------------------- <
lea ecx,[str] ; buffer, i.e. string source
> ;;; mov edx,[str]
> ;;; mov ecx,edx
> mov edx,2
> mov eax,4
> mov ebx,1
> int 0x80
> ------------------------------------------------ <
verify:
where is your buffer :)
how is the buffer related to your data?
what about EDI ?
> ------------------------------------------------ <
push dword 0x0a0d ; push immediate data, lsb at addr ESP
mov ecx,esp ; buffer is top dword of r-stack
> ;;; mov edi,0x0a0d00
> ;;; mov ecx,edx
> mov edx,2
> mov eax,4
> mov ebx,1
> int 0x80
lea esp,[esp+4] ; discard temp. buffer
> ret
>
> exit:
>
> mov ebx,eax
> mov eax,1
> int 0x80
> ------------------------------------------------ <
best,
hp
--
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml en/de
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-11-04 13:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-02 2:12 appending \n using registers Jason Roberts
2003-11-02 19:15 ` peter w krause
2003-11-03 11:21 ` peter w krause
[not found] <Law9-F57R88vo2O66rD0001ae77@hotmail.com>
2003-11-03 19:55 ` wklux
[not found] <Law9-F43TlbI3c57jHV0001de3a@hotmail.com>
2003-11-04 13:30 ` peter w krause
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.