Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie NASM question
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Wed Apr 18, 2012 4:22 pm    Post subject: Reply with quote

Ah yes, the limit was due to using word sizes. Change this bit of the code (in lib_print_number):

Code:
        mov eax, 0                      ; Start converting number to ASCII
        mov word ax, [loopctr]


to just:

Code:
        mov dword eax, [loopctr]


As for passing the limit as a parameter, yep, that's also doable. But bear in mind that the limit number will be passed as an ASCII text string, so you can't use it directly. Instead, you need to convert it from a string to a proper number; there's code for this in lib_input_number in library.asm.

Give it a try, good luck, and let me know if you need any help!

M
Back to top
View user's profile Send private message Visit poster's website
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Thu Apr 19, 2012 7:52 am    Post subject: Reply with quote

M-Saunders wrote:
Ah yes, the limit was due to using word sizes. Change this bit of the code (in lib_print_number):

Code:
        mov eax, 0                      ; Start converting number to ASCII
        mov word ax, [loopctr]


to just:

Code:
        mov dword eax, [loopctr]


As for passing the limit as a parameter, yep, that's also doable. But bear in mind that the limit number will be passed as an ASCII text string, so you can't use it directly. Instead, you need to convert it from a string to a proper number; there's code for this in lib_input_number in library.asm.


Give it a try, good luck, and let me know if you need any help!



M


Cheers Mike, I do appreciate your help, how do you know what registers to put what in ( if that makes sense ) I've downloaded a few articles from the web and none of them state in muppet terms "this must go in eax","this must be a dword" etc etc... I'm finding it difficult to self help ( I'm sure you dont want me bugging you every day ) ]'m a pro dev of long standing but never got involved with assembler as I never had a need to ( nor do I now but it's fascinating ) keep up the tutorials !!
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Thu Apr 19, 2012 12:39 pm    Post subject: Reply with quote

pk_fox wrote:
Cheers Mike, I do appreciate your help, how do you know what registers to put what in ( if that makes sense ) I've downloaded a few articles from the web and none of them state in muppet terms "this must go in eax","this must be a dword" etc etc...


That's impossible to say -- it depends entirely what you want to do. All good routines and libraries should document which registers they use to receive data, and which registers they use to pass data back. For instance, the comment section at the top of library.asm tells you which registers are used by the corresponding routines.

M
Back to top
View user's profile Send private message Visit poster's website
pk_fox
LXF regular


Joined: Wed Apr 13, 2005 2:38 pm
Posts: 239
Location: Surrey, UK

PostPosted: Thu Apr 19, 2012 1:14 pm    Post subject: Reply with quote

pk_fox wrote:
M-Saunders wrote:
Ah yes, the limit was due to using word sizes. Change this bit of the code (in lib_print_number):

Code:
        mov eax, 0                      ; Start converting number to ASCII
        mov word ax, [loopctr]


to just:

Code:
        mov dword eax, [loopctr]


As for passing the limit as a parameter, yep, that's also doable. But bear in mind that the limit number will be passed as an ASCII text string, so you can't use it directly. Instead, you need to convert it from a string to a proper number; there's code for this in lib_input_number in library.asm.


Give it a try, good luck, and let me know if you need any help!



M


Cheers Mike, I do appreciate your help, how do you know what registers to put what in ( if that makes sense ) I've downloaded a few articles from the web and none of them state in muppet terms "this must go in eax","this must be a dword" etc etc... I'm finding it difficult to self help ( I'm sure you dont want me bugging you every day ) ]'m a pro dev of long standing but never got involved with assembler as I never had a need to ( nor do I now but it's fascinating ) keep up the tutorials !!



Me again !, can't get lib_input_number to play ball Sad this is what I've tried

_start:
mov dword [loopctr],0
;mov dword [looplimit],100
pop ebx ; argc (argument count)
pop ebx ; argv[0] (argument 0, the program name)
pop ebx ; The first real arg, a filename
mov [filename],ebx
pop ebx ; extra pop for looplimit param
mov [buffer],ebx ; then move it to your buffer var
call lib_input_number
mov [looplimit],eax ; is this correct ? where is the converted number held ?
mov ebx,[filename]

; rest of the code unchanged.

; modded lib_input_number

lib_input_number:
pusha

;mov edi, buffer ; already populated by me
;call lib_input_string ; don't need this call ?

mov esi, buffer ; buffer has the last popped ebx

mov eax, 0 ; Initialise accumulator
mov ebx, 10 ; Decimal-system's radix

.next_digit:
mov cl, [esi] ; Fetch next character
cmp cl, '0' ; Char preceeds '0'?
jb .invalid ; Yes, not a numeral
cmp cl, '9' ; Char follows '9'?
ja .invalid ; Yes, not a numeral
mul ebx ; Ten times prior sum
and ecx, 0x0F ; Convert char to int
add eax, ecx ; Add to prior total
inc esi ; Advance source index
jmp .next_digit ; And check another char

.invalid:
mov long [tmp], eax ; Get EAX back after popa
popa
mov long eax, [tmp]
ret


sorry to be a pain
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Thu Apr 19, 2012 4:23 pm    Post subject: Reply with quote

pk_fox wrote:
can't get lib_input_number to play ball Sad


Please be much more specific! Does it not work at all? Does it return the wrong number? What exactly happens? Does the program crash?

pk_fox wrote:
call lib_input_number
mov [looplimit],eax ; is this correct ? where is the converted number held ?


library.asm tells you: "Store positive integer in EAX". Please always read the documentation at the start.

pk_fox wrote:
; modded lib_input_number


How did you modify it? It's very difficult for me to bugfix something when you've changed the routines and not described how...

M
Back to top
View user's profile Send private message Visit poster's website
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast