Newbie NASM question

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Postby M-Saunders » Wed Apr 18, 2012 3:22 pm

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

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


to just:

Code: Select all
        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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Postby pk_fox » Thu Apr 19, 2012 6:52 am

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

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


to just:

Code: Select all
        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 !!
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby M-Saunders » Thu Apr 19, 2012 11:39 am

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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Postby pk_fox » Thu Apr 19, 2012 12:14 pm

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: Select all
        mov eax, 0                      ; Start converting number to ASCII
        mov word ax, [loopctr]


to just:

Code: Select all
        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 :-( 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
User avatar
pk_fox
LXF regular
 
Posts: 298
Joined: Wed Apr 13, 2005 1:38 pm
Location: Surrey, UK

Postby M-Saunders » Thu Apr 19, 2012 3:23 pm

pk_fox wrote:can't get lib_input_number to play ball :-(


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
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Previous

Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests