Author Topic: Assembly Programming Thread  (Read 2230 times)

I have an intro to Assembly Programming class this semester. We just completed our first programs and it was super tedious. Yet strangely satisfying when I finished it. I was wondering how many other people have experience with it. What tips they could offer.

I'm doing all of my work on a virtual box setup with 32 bit windows xp with an intel core. Also using NASM to compile it all.

So here is my first super simple yet super long program

Code: [Select]
; Patrick McPartland
; Program:  Assignment 2: Simple arithmetic
; & Input/Output
%include "asm_io.inc"

segment .data
prompt1 db "Please enter an integer: ", 0
prompt2 db "Please enter a second intger: ",0

sumMsg1 db "The sum ",0
sumMsg2 db " + ",0
sumMsg3 db " is: ",0

difMsg1 db "The difference is: ",0
difMsg2 db " - ",0
difMsg3 db " is: ",0

productMsg1 db "The product ",0
productMsg2 db " x ",0
productMsg3 db " is: ",0

divMsg1 db "The integer quotient ",0
divMsg2 db " / ",0
divMsg3 db " is: ",0

segment .bss
integer1 resd 1
integer2 resd 1

segment .text
global _asm_main

_asm_main:
enter 0,0
pusha

;Input prompts
mov eax, prompt1
call print_string

call read_int
mov [integer1], eax

mov eax, prompt2
call print_string

call read_int
mov [integer2], eax

;Addition math
mov eax, [integer1]
add eax, [integer2]
mov ebx, eax

;Output of addition
mov eax,sumMsg1
call print_string
mov eax, [integer1]
call print_int
mov eax, sumMsg2
call print_string
mov eax, [integer2]
call print_int
mov eax, sumMsg3
call print_string


mov eax,ebx
call print_int
call print_nl

;Subtraction math
mov  eax, [integer1]
sub eax, [integer2]
mov ebx, eax

;Output of subtraction
mov eax,difMsg1
call print_string
mov eax, [integer1]
call print_int
mov eax, difMsg2
call print_string
mov eax, [integer2]
call print_int
mov eax, difMsg3
call print_string

mov     eax, ebx
call print_int
call print_nl

;Multiplication math
mov eax,[integer1]
mov ebx,[integer2]
mul ebx
mov ebx, eax

;Output of multiplication
mov eax,productMsg1
call print_string
mov eax, [integer1]
call print_int
mov eax, productMsg2
call print_string
mov eax, [integer2]
call print_int
mov eax, productMsg3
call print_string

mov     eax, ebx
call print_int
call print_nl

;Integer division math
mov eax, [integer1]
mov ebx, [integer2]
idiv ebx
mov ebx, eax

;Output of integer division
mov eax,divMsg1
call print_string
mov eax, [integer1]
call print_int
mov eax, divMsg2
call print_string
mov eax, [integer2]
call print_int
mov eax, divMsg3
call print_string

mov eax, ebx
call print_int

popa
mov eax, 0
leave
ret

Here is what it outputs along with compiling it.

Real men code in machine code.

Real men code in machine code.

Assembly compiles 1 to 1 in machine code, right?  So I pretty much already am!

Code: [Select]
num1 = raw_input("Please enter an integer: ")
num2 = raw_input("Please enter a second integer: ")
print "The sum of " + num1 + " and " + num2 + " is: " int(num1) + int(num2)
print "The difference of " + num1 + " and " + num2 + " is: " int(num1) - int(num2)
print "The product of " + num1 + " and " + num2 + " is: " int(num1) * int(num2)
print "The quotient of " + num1 + " and " + num2 + " is: " int(num1) / int(num2)

python

rele kids HACK IN CMD!!!

010100100110010101100001011011000010000001101101011001010110111000100000011000110110111101100100011001010010000001101001011011100010000001101101011000010110001101101000011010010110111001100101001000000110001101101111011001000110010100101110
011010010110111001100100011001010110010101100100001011100010111000101110

rele kids HACK IN CMD!!!

I have a feeling you have no clue what the thread is about.

I've wanted to get into assembly, but x86 asm is just so unreadable that I just can't get started. 

011010010110111001100100011001010110010101100100001011100010111000101110

Binary is not the same as machine code.

I have a feeling you have no clue what the thread is about.

I've wanted to get into assembly, but x86 asm is just so unreadable that I just can't get started. 

nal's just doing his handicapped thing he does whenever he sees something that's "edgy", "hipster", or "sup3r hack3r".
this isn't new :(

I have a feeling you have no clue what the thread is about.

I've wanted to get into assembly, but x86 asm is just so unreadable that I just can't get started.  

I really doubt he knows how to do much. But x86 is 64 bit, right? Yeah we're only doing 32 bit because he said that 64 is super confusing.


nal's just doing his handicapped thing he does whenever he sees something that's "edgy", "hipster", or "sup3r hack3r".
this isn't new :(

He just wants to feel cool because he can open a command terminal.

Code: [Select]
Extremely long block of code

assembly
Code: [Select]
num1 = raw_input("Please enter an integer: ")
num2 = raw_input("Please enter a second integer: ")
print "The sum of " + num1 + " and " + num2 + " is: " int(num1) + int(num2)
print "The difference of " + num1 + " and " + num2 + " is: " int(num1) - int(num2)
print "The product of " + num1 + " and " + num2 + " is: " int(num1) * int(num2)
print "The quotient of " + num1 + " and " + num2 + " is: " int(num1) / int(num2)

python
This is why I really don't want to learn assembly. There many other languages that are easier, more readable, and more flexible.
(Yes, I realize all code just gets compiled into assembly, but I'm talking about writing it by hand.)

nal's just doing his handicapped thing he does whenever he sees something that's "edgy", "hipster", or "sup3r hack3r".
this isn't new :(
Do we have to go over this again
He just wants to feel cool because he can open a command terminal.
I was kidding

This is why I really don't want to learn assembly. There many other languages that are easier, more readable, and more flexible.
(Yes, I realize all code just gets compiled into assembly, but I'm talking about writing it by hand.)

I agree with everything, except for the flexible part. With assembly you can do anything you want. It's way more flexible than any other language in existence.

The only reason we're required to take assembly is because using assembly we can make C and C++ way more efficient with it. It lets us go down to the assembly level and change certain functions so they run better. If you're interested in drivers you're gonna have to know how to use assembly.

It was also strangely satisfying to finish an assembly program. I'll most likely never use it for anything serious.

I agree with everything, except for the flexible part. With assembly you can do anything you want. It's way more flexible than any other language in existence.

The only reason we're required to take assembly is because using assembly we can make C and C++ way more efficient with it. It lets us go down to the assembly level and change certain functions so they run better. If you're interested in drivers you're gonna have to know how to use assembly.

It was also strangely satisfying to finish an assembly program. I'll most likely never use it for anything serious.
I suppose you're right about the flexibility, but if you plan on writing it by hand it's kinda improbable that you can dive that deep.

I suppose you're right about the flexibility, but if you plan on writing it by hand it's kinda improbable that you can dive that deep.

Lot's of people write things in assembly. It's just hard.