WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: qweac.sh :A Quickly Way to Edit Assembly Code  (Read 3168 times)

Offline ferran

  • Full Member
  • ***
  • Posts: 159
qweac.sh :A Quickly Way to Edit Assembly Code
« on: April 29, 2020, 01:22:27 PM »
This is an opened project focused to make a little bit more easy the of assembler code editing of .asm files


Usually assembly programs (.asm format with nasm, masm, gas, yasm, etc.) are very long because only one instruction is allowed in each line of code. I thought of finding a way to solve this and here it is.

For instance: This assembly  program will shows one line once executed

file sample.asm

Code: [Select]
SECTION .data
msg dd "Editing successful!",0xa
len equ $ - msg

SECTION .text
global _start

_start:
;; sys_write(edx,len=length ; ecx=message ; ebx,1=descriptor 1 ; eax,4=screen)
mov edx,len
mov ecx, msg
mov ebx,1
mov eax,4 
int 0x80

;; exit
mov eax,1
mov ebx,0
int 0x80

Could the same code be edited differently? The answer is yes:

file: sample.txt

Code: [Select]
SECTION .data
\t msg dd "Editing successful!",0xa
\t len equ $ - msg

SECTION .text
global _start

\t\t _start:
;; sys_write(edx,len=length ; ecx=message ; ebx,1=descriptor 1 ; eax,4=screen)
\t\t mov edx,len \n\t\t mov ecx, msg \n\t\t mov ebx,1 \n\t\t mov eax,4 
\t\t int 0x80

;; exit
\t\t mov eax,1 \n\t\t mov ebx,0
\t\t int 0x80

The difference is that you can add tabulators "\t" and new line "\n" characters between instructions, labels and sections.

This would be impossible to compile with gcc in this way, unless you use a program like this:

Code: [Select]
#!/bin/bash
#########################################################
# Title: qweac.sh                                                                                     
# Description: Quickly Way of Edit Assembly Code (qweac)                 
# Version: 0.1 beta                                                             
# Author: Ferran (Tiny Core Linux' user)
# Original-Site: http://forum.tinycorelinux.net/            
# Size:1.3 Kb
# #Comments:
# A tool that allows you to compile assembly code in an original way   
# (as explained in the Tiny Core Linux forum web) making your own
# ELF binaries files from automatic pre-compiled sample.asm files.
#_________
#qweac.sh requieres bash.tcz, gcc.tcz & nasm.tcz
# _________
# Current: 2020/04/29 (First Beta Version)
#########################################################

index=0
unset array
while IFS= read -r; do
array[index+=1,$index]=$REPLY
done <$1

[[ $REPLY ]] && array[index]=$REPLY

for line in `seq 0 ${#array[*]}`;
do
     echo -e "${array[$line]}" >> sample.asm
done   

nasm -felf32  sample.asm
gcc  -nostdlib -m32 sample.o -o sample

TIP for the people:

If you aren't familiar with assembly code, after to download this code as if you will execute it with

Code: [Select]
./qweac.sh sample.txt    #If you wants use the above file sample.txt

It will make 3 more files (f.i): sample.asm, sample.o and sample (as if). If you can see the message without issues. Then you already will run it with

Code: [Select]
$ ./sample
$ Editing successful!
$

This program will be compatible in the most computers around the world.

NOTE: At the moment you should use 32-bit assembly code, intended for x386 architectures

Basically still have a couple of things to do yet:

1) Be able to compile .asm in 64 bits

2) The name of the sample file (f.i. myfile.txt) is the same as the .asm file (such as myfile.asm, myfile.o and the executable ELF file).

If you want to help, complain, provide solutions, give me a house with jacuzzi, etc. you are welcome.

« Last Edit: April 29, 2020, 02:38:16 PM by Rich »
TC CorePlus v.11.1 i686 & lots of coffe

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: qweac.sh :A Quickly Way to Edit Assembly Code
« Reply #1 on: April 29, 2020, 02:25:12 PM »
@Rich or juanito

I forgot to stand #!/bin/bash in line 1 in qweac.sh code when I did the comments, now i can't to edit it.


« Last Edit: April 29, 2020, 02:35:52 PM by ferran »
TC CorePlus v.11.1 i686 & lots of coffe

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: qweac.sh :A Quickly Way to Edit Assembly Code
« Reply #2 on: April 29, 2020, 02:39:02 PM »
Hi ferran
I forgot to stand #!/bin/bash in line 1 in qweac.sh code when I did the comments, now i can't to edit it.
Fixed. :)

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: qweac.sh :A Quickly Way to Edit Assembly Code
« Reply #3 on: April 29, 2020, 07:47:06 PM »
Thank you Rich.

So I have found the solution to separate the name of the initial file (and join it with .asm to get the new file to be compiled). And I have also managed to find a command that informs which is the architecture that the computer has to compile and how it should do it.

All these improvements are already present in the new version. Also I wrote several comments. Here it go:

Code: [Select]
#!/bin/bash
#########################################################
# Title: qweac.sh                                                                                     
# Description: Quickly Way of Edit Assembly Code (qweac)                 
# Version: 0.2 beta                                                             
# Author: Ferran (Tiny Core Linux' user)
# Original-Site: http://forum.tinycorelinux.net/            
#Size:1.3Kb
#Comments
# A tool that allows you to compile assembly code in an original way   
# (as explained in the Tiny Core Linux forum web) making your own
# ELF binaries files from automatic pre-compiled sample.asm files.
#_________
# qweac.sh requieres bash.tcz, gcc.tcz & nasm.tcz
#_________
#Current: 2020/04/29 (Second Beta Version)    
#########################################################

# Get the line's file and store it into an array

index=0
unset array
while IFS= read -r; do
array[index+=1,$index]=$REPLY
done <$1

[[ $REPLY ]] && array[index]=$REPLY

# Get the file name (separated from its extension)
# and after it will be joined to .asm extension

filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
fileasm=$filename".asm"

# Cross the array to build the file .asm

for line in `seq 0 ${#array[*]}`;
do
     echo -e "${array[$line]}" >> $fileasm
done 

# Test & select witch architecture it's needed
# to compile the file.asm

if [ "$(getconf LONG_BIT)" == "32" ]
then
nasm -felf32  sample.asm
gcc  -nostdlib -m32 sample.o -o sample
else
nasm -felf64  -o sample.o sample.asm
ld  -o sample sample.o
fi

That's enough for today  ;D
« Last Edit: April 29, 2020, 07:51:46 PM by ferran »
TC CorePlus v.11.1 i686 & lots of coffe

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: qweac.sh :A Quickly Way to Edit Assembly Code
« Reply #4 on: April 30, 2020, 09:30:51 AM »
Version 0.3 : Final version by the moment.

Hello again folks!

Yesterday I was tired and I didn't make some changes I should have made. I'm sorry I have to repeat the post to fix the arrangements.

However, I have improved the automation of the compilation process, so this revision of version 0.2 is better finished, so it becomes version 0.3

Code: [Select]
#!/bin/bash
#########################################################
# Title: qweac.sh                                                                                     
# Description: Quickly Way of Edit Assembly Code (qweac)                 
# Version: 0.3 beta                                                             
# Author: Ferran (Tiny Core Linux' user)
# Original-Site: http://forum.tinycorelinux.net/            
# Size: 1.7 Kb
#        
# Comments: :
#        
# A tool that allows you to compile assembly code in an original way   
# (as explained in the Tiny Core Linux forum web) making your own
# ELF binaries files from automatic pre-compiled sample.asm files.
# _________
# qweac.sh requieres bash.tcz, gcc.tcz & nasm.tcz
# _________
# Current: 2020/04/30 (Final Beta Version)    
#########################################################

# Get the line's file and store it into an array

index=0
unset array
while IFS= read -r; do
array[index+=1,$index]=$REPLY
done <$1

[[ $REPLY ]] && array[index]=$REPLY

# Get the file name (separated from its extension)
# and after it will be joined to .asm extension

filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
fileasm=$filename".asm"
fileobj=$filename".o"
arch=$(getconf LONG_BIT)

# Cross the array to build the file .asm

for line in `seq 0 ${#array[*]}`;
do
     echo -e "${array[$line]}" >> $fileasm
done 

#Compiling process

nasm -f elf$arch $fileasm
gcc -nostdlib -m$arch $fileobj -o $filename

I apologize for these little inconveniences. Enjoy this arrangement.
TC CorePlus v.11.1 i686 & lots of coffe