Bryan
Bryan

Reputation: 33

What is the need for this step in this assembly code - x86

Here is a program for the user to input some characters and it will output whatever the user types in.

    ; Section to store variables
    section .data
    string_msg: db "Enter a string (Max=64 Characters)", 0xD, 0
    string_in: times 65 db 0h ; max = 64 char, last ch=null
    string_out: times 65 db 0h ; max = 64 char, last ch=null

    ; Start of program
    global _main
    section .text
    _main:
    mov ebp, esp; for correct debugging

    PRINT_STRING string_msg
    NEWLINE
    GET_STRING string_in, 65

   ; init value to process input string
   xor ebx, ebx

   start:
   mov al, byte[string_in  + ebx]
   mov byte[string_out + ebx], al

   PRINT_CHAR al
   cmp al, 0h
   je end
   inc ebx
   jmp start

   end: 
   xor eax, eax    ; terminate program
   ret 

What I do not understand is why is the mov byte[string_out + ebx], al there for? What does it mean?

Thank you very much for your help!

Upvotes: 2

Views: 153

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

The instruction that you are referring to (mov byte[string_out + ebx], al), is just making a copy of the inputted string.

Since the string is displayed one character at a time using PRINT_CHAR, the display doesn't really need it.

If ever this program is expanded to use an alternative output method, then this copy might become just what is needed.

PRINT_STRING string_out

PRINT_CHAR al
cmp al, 0h
je end

This could be a small error! The terminating zero should normally not be displayed on screen. The test would better be done before printing.

cmp al, 0
je  end
PRINT_CHAR al

Upvotes: 2

Related Questions