im having some trouble writing my own atoi function in assembly. The instructions are
"Change the function so that it returns the integer equivalent of the C-string (pointer) that is passed into the function. You may assume that the first character is between ‘0’ and ‘9’, inclusive. atoishould consider all the characters from the first up until the first character that is not a decimal digit. As you can see, mainuses the value returned by atoias an exit code (this is just a cheap way of accessing the output from atoi, without writing an itoafunction.) As given to you, atoireturns 1234. The return value is ANDedwith 0xFF to reduce it to a byte. Thus 1234 & 255 becomes 210."
# Useful constants
.equ STDIN,0
.equ STDOUT,1
.equ READ,0
.equ WRITE,1
.equ EXIT,60
# Stack frame
.equ bufferSize, 32
.equ buffer,-bufferSize
.equ localSize,16
.equ frameSize, bufferSize + localSize
# Read only data
.section .rodata # the read-only data section
prompt:
.string "Enter an integer: "
.equ promptSz,.-prompt-1
msg:
.string "You entered: "
.equ msgSz,.-msg-1
Code
.text # switch to text section
.globl __start
__start:
pushq %rbp # save caller’s frame pointer
movq %rsp, %rbp # establish our frame pointer
subq $frameSize, %rsp # for local variables
movl $promptSz, %edx # prompt size
movl $prompt, %esi # address of prompt text string
movl $STDOUT, %edi # standard out
movl $WRITE, %eax
syscall # request kernel service
movl $bufferSize,%edx
leaq buffer(%rbp), %rsi # load buffer address
movl $STDIN, %edi # standard in
movl $READ, %eax
syscall # request kernel service
movl %eax, (%rsp) # store num chars read
leaq buffer(%rbp), %rsi # load buffer address
call atoi # our exit code will be the return from atoi
movq %rbp, %rsp # delete local variables
popq %rbp # restore caller’s frame pointer
movl %eax, %edi # put exit status in %edi (will be ANDed with FF)
movl $EXIT, %eax # exit from this process
syscall
the base code looks like this where i just have to implement my own atoi. so far what i have for the atoi function is
atoi:
pushq %rbp # save caller’s frame pointer
movq %rsp, %rbp # establish our frame pointer
subq $16, %rsp # for local variables
movq %rdi, -16(%rbp) #moving first argument to local variable
movl $0, -4(%rbp) #moving 0 to local variable
movl $10, -12(%rbp) #moving 10 to local variable
movl -16(%rbp), %rax
movzbl (%rax), %eax #getting value of rax
movl -4(%rbp), %eax
imull -12(%rbp), %eax
movl %eax, -4(%rbp)
movq %rbp, %rsp # delete local variables
popq %rbp # restore caller’s frame pointer
ret
im at a loss for where to go next. it seems anything i do just gives me segmentation faults