---
# System prepended metadata

title: Printing each characters in string
tags: [Assembly]

---




# Code
```Assembly
section .data
    length1 equ 255
    length2 equ 255
section .bss
    msg1 resb 255
    msg2 resb 255
section .text
    global _start
_start:
    mov ecx, msg1   
    mov edx, length1 
    mov eax, 3
    mov ebx, 2
    int 0x80

    sub eax, 1
    xor esi, esi
    xor edi, edi
    execuate:
        mov dl, [msg1+esi]
        inc esi
        cmp dl, 0x61
        jnl condition1
        cmp dl, 0x41
        jnl condition2

        execuate2:
        cmp esi, eax
        jnl print
        
    loop execuate
print:
    mov edx, edi
    mov ecx, msg2
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov eax, 1
    int 0x80
ret
condition1:
    cmp dl, 0x7a
    jna transfer_to_msg2
    jmp execuate2
ret
condition2:
    cmp dl, 0x5a
    jna transfer_to_msg2
    jmp execuate2
ret
transfer_to_msg2:
    mov [msg2+edi], dl
    inc edi
    jmp execuate2
ret
```
## Idea
- It is undeniable that al, dl, cl, bl has 8 bits, so I will use those registers for manipulating easily because 1 byte = 8 bits.

- Warning: it is compulsory to use 8 bits registers as they just take 1 character while ebx, eax, ecx, edx take even 4 characters (32bits = 4 bytes = 4 chars).
- Finally, you just transfer characters to new string, then print it.

![Untitled](https://hackmd.io/_uploads/BkUtSPIcp.png)
## Data
- First of all, I set up section .data and .bss.
- As we do not how many characters in a string inputted, thus I will allocate storage space for unintialized data at 255.
=> About length of string, I think it depends on the topic.
```Assembly
section .data
    length1 equ 255
    length2 equ 255
section .bss
    msg1 resb 255
    msg2 resb 255
```
## Input:
- Secondly, I just get inputted according to topic.

```Assembly
section .text
    global _start
_start:
    mov ecx, msg1   
    mov edx, length1 
    mov eax, 3
    mov ebx, 2
    int 0x80
```

## Execuate:
```Assembly
       sub eax, 1
    xor esi, esi
    xor edi, edi
    execuate:
        mov dl, [msg1+esi]
        inc esi
        cmp dl, 0x61
        jnl condition1
        cmp dl, 0x41
        jnl condition2

        execuate2:
        cmp esi, eax
        jnl print
        
    loop execuate
print:
    mov edx, edi
    mov ecx, msg2
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov eax, 1
    int 0x80
ret
condition1:
    cmp dl, 0x7a
    jna transfer_to_msg2
    jmp execuate2
ret
condition2:
    cmp dl, 0x5a
    jna transfer_to_msg2
    jmp execuate2
ret
transfer_to_msg2:
    mov [msg2+edi], dl
    inc edi
    jmp execuate2
ret
```

- It is clear that eax always has the length of string after getting inputted, so I just minus 1 for being correct.

![image](https://hackmd.io/_uploads/rJQ8bzS5T.png)

![image](https://hackmd.io/_uploads/BkzFZzSqa.png)

- I prior to using XOR, it is same as mov esi, 0 but it is more thorough.

- According to idea which I said before, I will use 8 bits registers, therefore I move the address of msg (0x804a000) into dl.

- Explain: it is certainly same as initialized data, each characters totally take 1 byte, so they will occupy the address of memory respectively, starting from 0x804a000.

2 -> 0x804a000
3 -> 0x804a001
t -> 0x804a002
.....

- After moving into dl, I increase esi, then compare it with 'A'->'Z'(0x41->0x5a) and 'a'->'z'(0x61->0x7a)

=> Just avoid those scopes.
```asm
     sub eax, 1
    xor esi, esi
    xor edi, edi
    execuate:
        mov dl, [msg1+esi]
        inc esi
        cmp dl, 0x61
        jnl condition1
        cmp dl, 0x41
        jnl condition2

        execuate2:
        cmp esi, eax
        jnl print
        
    loop execuate
print:
    mov edx, edi
    mov ecx, msg2
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov eax, 1
    int 0x80
ret
```
```asm
condition1:
    cmp dl, 0x7a
    jna transfer_to_msg2
    jmp execuate2
ret
condition2:
    cmp dl, 0x5a
    jna transfer_to_msg2
    jmp execuate2
ret
transfer_to_msg2:
    mov [msg2+edi], dl
    inc edi
    jmp execuate2
ret
```
- If your dl satisfied with condition established, it would begin transferring to msg2.
- I created new string named msg2 in addition of edi starting from 0.

```asm
transfer_to_msg2:
    mov [msg2+edi], dl
    inc edi
    jmp execuate2
ret
```

- Then just simply increase edi for next characters.

- After that, I will come back to execuate procedure created, and continue looping with new address of character which now has esi = 1.

- Subsequently, I compare dl like I said above and transfer each characters to msg2 respectively.

```asm
    sub eax, 1
    xor esi, esi
    xor edi, edi
    execuate:
        mov dl, [msg1+esi]
        inc esi
        cmp dl, 0x61
        jnl condition1
        cmp dl, 0x41
        jnl condition2

        execuate2:
        cmp esi, eax
        jnl print
        
    loop execuate
```

- Finally, I just wait for the escalation of esi being greater or equal to eax (length of msg1), later on printing msg2.
```Asm
print:
    mov edx, edi
    mov ecx, msg2
    mov ebx, 1
    mov eax, 4
    int 0x80

    mov eax, 1
    int 0x80
ret
```
# Result:
![image](https://hackmd.io/_uploads/B1CULRdoT.png)







