0

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!

  • 2
    This is using 32-bit system calls. You should probably build it into a 32-bit executable (`nasm -felf32` / `gcc -m32 -static -nostdlib foo.o` or `ld -melf_i386 foo.o`). Otherwise, if you want to write 64-bit code, see [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) for why `int 0x80` is the wrong system-call interface to use. – Peter Cordes Oct 16 '20 at 06:34
  • 1
    Looks like all you're missing is [NASM Assembly convert input to integer?](https://stackoverflow.com/q/19309749) for converting an input ASCII string of digits into an integer. IDK why you're only printing 1 char at a time, though. `read` gives you a return value (number of bytes read), from which you can calculate total - offset = length to use for the 2nd write, after writing the whole thing. – Peter Cordes Oct 16 '20 at 06:37
  • 1
    "doesn't work" isn't a useful problem description, and you haven't shown the code that doesn't work either. Create a [mcve] of that attempt, including exactly what happens when you try it, if you still need help using the string->int conversion result. – Peter Cordes Oct 16 '20 at 11:20

0 Answers0