1

I'm gathering an input from the user (a string they wish to use), I then want to ask them what offset they'd like to use after storing this I know it's stored as a string, this isn't much use to me as I need to be able to use the value in a loop. Is there some way that I can convert the string for example ("2") to 2?

I have gathered an input from the user and a offset value, I then have stored these values

SECTION .data
userMsg db 'Please enter a name: ', 0
userMsgLen equ $-userMsg 
valMsg db 'Offset value: '
valMsgLen equ $-valMsg
s1 resb 10

valStore resb 255
userStore resb 255

SECTION .bss
s2 resb 10

SECTION .text
global  _start
 
_start:
mov edx, userMsgLen
mov ecx, userMsg 
mov ebx, 1 
mov eax, 4 
int 80h 

; Read the input from the keyboard
mov edx, 255        
mov ecx, userStore   
mov ebx, 0          
mov eax, 3          
int 80h 

mov edx, valMsgLen
mov ecx, valMsg 
mov ebx, 1 
mov eax, 4 
int 80h 

; Read the input from the keyboard
mov edx, 255        
mov ecx, valStore   
mov ebx, 0          
mov eax, 3          
int 80h 

mov ecx, userMsg
mov esi, s1
mov edi, s2
    
; loop here
         

I want to convert the value of valStore to a integer so it can be used in a loop to add the value of valStore

Loop ideas from here

JakkuPingu
  • 23
  • 3
  • 1
    The standard algorithm is to work with an accumulated value originally set to `0`. For each incoming digit, you multiply the accumulator by `10` and add the (ASCII-adjusted) digit value. – Weather Vane Mar 02 '23 at 20:08
  • "convert the string for example ("2") to 2?" For a single digit, that is `ch - '0'`. Why? Consider if that `ch` is `'0'` then clearly its value is `ch - '0'` which is `0`. Because the 10 numeral characters are usually encoded in a consecutive increasing sequence, it follows that `'1' - '0'` is `1`, etc. – Weather Vane Mar 02 '23 at 20:10
  • The line that defines *nameMsg* is missing! – Sep Roland Mar 02 '23 at 20:29
  • @SepRoland my apologies that wasn't meant to be included – JakkuPingu Mar 02 '23 at 20:38
  • @WeatherVane Sorry if it's a stupid question, I'm very new to assembly. When replacing `add al, 02` with `add al, valStore` involving the loop from https://www.tutorialspoint.com/assembly_programming/assembly_lods_instruction.htm# it gives me a segmentation error, I'm assuming I'm missing some steps to define valStore as an int value or maybe calling into the relevant part of memory – JakkuPingu Mar 02 '23 at 20:45
  • There isn't an `add al,...` in the code posted. Adding a value to `al` cannot itself cause a segfault. Anyway, the accumulator will probably need to be bigger than 8 bits. But if the input is a single digit, then you don't need any accumulation, just subtract the character`'0'`. – Weather Vane Mar 02 '23 at 20:48
  • @WeatherVane I was referring to this loop loop_here: lodsb add al, 02 stosb loop loop_here cld rep movsb This is used to change the ascii value of userStore by 02 – JakkuPingu Mar 02 '23 at 20:58
  • A code to convert a string (like "2" or "12387") into a number is at https://stackoverflow.com/questions/22570524/how-to-read-and-display-a-value-in-linux-assembly/22578313?r=SearchResults&s=9%7C28.8080#22578313. – Sep Roland Mar 02 '23 at 21:00
  • 1
    @SepRoland: I prefer [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) as a canonical duplicate; the answer you linked unfortunately over-complicates the multiply by 10 by doing it with a mov/shl/2x add, instead of two `lea`s or an `imul`. And it's pretty sparely commented compared to Michael's 32-bit answer or my 64-bit answer on the NASM string-to-integer question. – Peter Cordes Mar 02 '23 at 21:42
  • @PeterCordes Indeed a better canonical. Would you believe that (https://stackoverflow.com/a/49548057) was the one I had in mind when I started searching? But I've never been good at finding... – Sep Roland Mar 02 '23 at 21:57
  • @SepRoland: My browser history auto-completion remembers a lot of ones I link frequently. If I hit control-l and type "nasm string to integer", it autocompletes to [How to convert user input of string to integer in assembly NASM](https://stackoverflow.com/q/72038946) (which is a duplicate of the one I want.) Or sometimes I end up at my answer on [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) and follow the link at the bottom of it. Or the FAQs section in in https://stackoverflow.com/tags/x86/info – Peter Cordes Mar 02 '23 at 22:05

0 Answers0