I'm working on an assignment for class where I have to take an 8 character string from the user and remove as many letters the user requests from the left side. This has to be done using a NASM Assembler to write x86-64 assembly. My code is as follows:
section .text
global _start
_start:
;PRINT "Enter Text: "
mov edx, lenMsg2
mov ecx, msg2
mov ebx, 1
mov eax, 4
int 80h
;READ AND STORE USER INPUT INTO str
mov eax, 3
mov ebx, 2
mov ecx, str
mov edx, 100
int 80h
;PRINT "Enter the number of characters to cut off: "
mov edx, lenMsg3
mov ecx, msg3
mov ebx, 1
mov eax, 4
int 80h
;READ AND STORE USER INPUT INTO num
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 100
int 80h
;PRINT "This is what you entered: "
mov edx, lenMsg4
mov ecx, msg4
mov ebx, 1
mov eax, 4
int 80h
;PRINT str
mov eax, 4
mov ebx, 1
mov ecx, str
mov edx, 100
int 80h
;PRINT "Here is your text edited: "
mov edx, lenMsg5
mov ecx, msg5
mov ebx, 1
mov eax, 4
int 80h
;LEFT TRUNCATE
mov esi, str
add esi, OFFSET_INTO_DATA ;NEED TO SOMEHOW REPLACE THIS WITH num
;RIGHT TRUNCATE
mov edi, NUM_CHARS
.loop:
mov ecx, esi
call print_char_32
inc esi
dec edi
jnz .loop
jmp halt
print_char_32:
mov edx, 1
mov ebx, 1
mov eax, 4
int 80h
ret
halt:
mov eax, 1
int 80h
jmp halt
section .data
;msg db "qwertyui" <- Test phrase used before str was made
msg2 db "Enter Text: ", 0x0A
lenMsg2 equ $ - msg2
msg3 db "Enter the number of characters to cut off: ",0x0A
lenMsg3 equ $ - msg3
msg4 db "This is what you entered: ", 0x0A
lenMsg4 equ $ - msg4
msg5 db "Here is your text edited: ", 0x0A
lenMsg5 equ $ - msg5
str: db 100
num: db 100
OFFSET_INTO_DATA EQU 5 ;5 was an test number used to subtract from str
NUM_CHARS EQU 8 ;This should print the whole string no matter how many
;characters the user removed
The result I'm hoping for should look like this below:
Enter Text:
*qwertyui*
Enter the number of characters to cut off:
*5*
Here is what you entered:
qwertyui
Here is your text edited:
yui
In place of 5, any number up to 8 should work. I have some arguments set up near the top for reading user input for num; however, I can't seem to just plug in num for where OFFSET_INTO_DATA is. Using OFFSET_INTO_DATA with the way I set it up in the data section lets the program run smoothly and gives me the result I want. It just doesn't work when I throw in num where it stands in the text section. Plugging in random numbers instead also doesn't work either, I'm not sure where to go from here.
I've been using nasm -f elf64 filename.asm and ld filename.o -o filename to compile this program. I'm also using PuTTY to run Linux. I'm not sure if these last bits of information help, but this is my first time using this site and I'd like to describe as much about my situation as possible. Any help is appreciated!