Assembly example

This is a discussion on Assembly example within the C Programming forums, part of the General Programming Boards category; people on this forum (back in october 2001) asked about assembly, since I just started revisiting the forums I guess ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Assembly example

    people on this forum (back in october 2001) asked about assembly, since I just started revisiting the forums I guess I can give a time-wasting example of a C++ compiled 8086asm example (compiled with g++)

    Code:
    	.file	"main.cpp"
    	.version	"01.01"
    gcc2_compiled.:
    		.section	.rodata
    .LC0:
    	.string	"Enter your first name: "
    .LC1:
    	.string	"Enter your last name: "
    .LC2:
    	.string	" "
    .LC3:
    	.string	" characters"
    .LC4:
    	.string	"Your first name has "
    .LC5:
    	.string	"Your last name has "
    .LC6:
    	.string	"Your name has a total of "
    .text
    	.align 4
    .globl main
    	.type	 main,@function
    main:
    .LFB1:
    	pushl	%ebp
    .LCFI0:
    	movl	%esp, %ebp
    .LCFI1:
    	subl	$168, %esp
    .LCFI2:
    	subl	$8, %esp
    	pushl	$.LC0
    	pushl	$cout
    .LCFI3:
    	call	__ls__7ostreamPCc
    	addl	$16, %esp
    	subl	$8, %esp
    	leal	-56(%ebp), %eax
    	pushl	%eax
    	pushl	$cin
    	call	__rs__7istreamPc
    	addl	$16, %esp
    	movl	$0, -12(%ebp)
    	.p2align 2
    .L3:
    	leal	-56(%ebp), %eax
    	movl	%eax, %edx
    	movl	-12(%ebp), %eax
    	cmpb	$0, (%eax,%edx)
    	jne	.L5
    	jmp	.L4
    	.p2align 2
    .L5:
    	subl	$12, %esp
    	leal	-56(%ebp), %eax
    	movl	%eax, %edx
    	movl	-12(%ebp), %eax
    	movsbl	(%eax,%edx),%eax
    	pushl	%eax
    	call	tolower
    	addl	$16, %esp
    	movl	%eax, -92(%ebp)
    	leal	-56(%ebp), %eax
    	movl	%eax, %edx
    	movl	-12(%ebp), %eax
    	movb	-92(%ebp), %cl
    	movb	%cl, (%eax,%edx)
    	leal	-12(%ebp), %eax
    	incl	(%eax)
    	jmp	.L3
    	.p2align 2
    .L4:
    	subl	$12, %esp
    	movsbl	-56(%ebp),%eax
    	pushl	%eax
    	call	toupper
    	addl	$16, %esp
    	movl	%eax, -96(%ebp)
    	movb	-96(%ebp), %al
    	movb	%al, -56(%ebp)
    	movl	$0, -16(%ebp)
    	subl	$8, %esp
    	pushl	$.LC1
    	subl	$12, %esp
    	pushl	$endl__FR7ostream
    	pushl	$cout
    .LCFI4:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$20, %esp
    	movl	%eax, -100(%ebp)
    	pushl	-100(%ebp)
    .LCFI5:
    	call	__ls__7ostreamPCc
    	addl	$16, %esp
    	subl	$8, %esp
    	leal	-88(%ebp), %eax
    	pushl	%eax
    	pushl	$cin
    	call	__rs__7istreamPc
    	addl	$16, %esp
    	.p2align 2
    .L6:
    	leal	-88(%ebp), %eax
    	movl	%eax, %edx
    	movl	-16(%ebp), %eax
    	cmpb	$0, (%eax,%edx)
    	jne	.L8
    	jmp	.L7
    	.p2align 2
    .L8:
    	subl	$12, %esp
    	leal	-88(%ebp), %eax
    	movl	%eax, %edx
    	movl	-16(%ebp), %eax
    	movsbl	(%eax,%edx),%eax
    	pushl	%eax
    	call	tolower
    	addl	$16, %esp
    	movl	%eax, -104(%ebp)
    	leal	-88(%ebp), %eax
    	movl	%eax, %edx
    	movl	-16(%ebp), %eax
    	movb	-104(%ebp), %cl
    	movb	%cl, (%eax,%edx)
    	leal	-16(%ebp), %eax
    	incl	(%eax)
    	jmp	.L6
    	.p2align 2
    .L7:
    	subl	$12, %esp
    	movsbl	-88(%ebp),%eax
    	pushl	%eax
    	call	toupper
    	addl	$16, %esp
    	movl	%eax, -108(%ebp)
    	movb	-108(%ebp), %al
    	movb	%al, -88(%ebp)
    	subl	$8, %esp
    	pushl	$endl__FR7ostream
    	subl	$12, %esp
    	leal	-88(%ebp), %eax
    	pushl	%eax
    	subl	$12, %esp
    	pushl	$.LC2
    	subl	$12, %esp
    	leal	-56(%ebp), %eax
    	pushl	%eax
    	subl	$12, %esp
    	pushl	$endl__FR7ostream
    	pushl	$cout
    .LCFI6:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$20, %esp
    	movl	%eax, -112(%ebp)
    	pushl	-112(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -116(%ebp)
    	pushl	-116(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -120(%ebp)
    	pushl	-120(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -124(%ebp)
    	pushl	-124(%ebp)
    .LCFI7:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$16, %esp
    	subl	$8, %esp
    	pushl	$endl__FR7ostream
    	subl	$12, %esp
    	pushl	$.LC3
    	subl	$12, %esp
    	pushl	-12(%ebp)
    	subl	$12, %esp
    	pushl	$.LC4
    	subl	$12, %esp
    	pushl	$endl__FR7ostream
    	pushl	$cout
    .LCFI8:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$20, %esp
    	movl	%eax, -128(%ebp)
    	pushl	-128(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -132(%ebp)
    	pushl	-132(%ebp)
    	call	__ls__7ostreami
    	addl	$20, %esp
    	movl	%eax, -136(%ebp)
    	pushl	-136(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -140(%ebp)
    	pushl	-140(%ebp)
    .LCFI9:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$16, %esp
    	subl	$8, %esp
    	pushl	$endl__FR7ostream
    	subl	$12, %esp
    	pushl	$.LC3
    	subl	$12, %esp
    	pushl	-16(%ebp)
    	subl	$12, %esp
    	pushl	$.LC5
    	pushl	$cout
    .LCFI10:
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -144(%ebp)
    	pushl	-144(%ebp)
    	call	__ls__7ostreami
    	addl	$20, %esp
    	movl	%eax, -148(%ebp)
    	pushl	-148(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -152(%ebp)
    	pushl	-152(%ebp)
    .LCFI11:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$16, %esp
    	subl	$8, %esp
    	pushl	$endl__FR7ostream
    	subl	$12, %esp
    	pushl	$.LC3
    	subl	$12, %esp
    	movl	-16(%ebp), %eax
    	addl	-12(%ebp), %eax
    	pushl	%eax
    	subl	$12, %esp
    	pushl	$.LC6
    	pushl	$cout
    .LCFI12:
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -156(%ebp)
    	pushl	-156(%ebp)
    	call	__ls__7ostreami
    	addl	$20, %esp
    	movl	%eax, -160(%ebp)
    	pushl	-160(%ebp)
    	call	__ls__7ostreamPCc
    	addl	$20, %esp
    	movl	%eax, -164(%ebp)
    	pushl	-164(%ebp)
    .LCFI13:
    	call	__ls__7ostreamPFR7ostream_R7ostream
    	addl	$16, %esp
    	movl	$0, %eax
    	leave
    	ret
    .LFE1:
    .Lfe1:
    	.size	 main,.Lfe1-main
    
    	.section	.eh_frame,"aw",@progbits
    __FRAME_BEGIN__:
    	.4byte	.LLCIE1
    .LSCIE1:
    	.4byte	0x0
    	.byte	0x1
    	.byte	0x0
    	.byte	0x1
    	.byte	0x7c
    	.byte	0x8
    	.byte	0xc
    	.byte	0x4
    	.byte	0x4
    	.byte	0x88
    	.byte	0x1
    	.align 4
    .LECIE1:
    	.set	.LLCIE1,.LECIE1-.LSCIE1
    	.4byte	.LLFDE1
    .LSFDE1:
    	.4byte	.LSFDE1-__FRAME_BEGIN__
    	.4byte	.LFB1
    	.4byte	.LFE1-.LFB1
    	.byte	0x4
    	.4byte	.LCFI0-.LFB1
    	.byte	0xe
    	.byte	0x8
    	.byte	0x85
    	.byte	0x2
    	.byte	0x4
    	.4byte	.LCFI1-.LCFI0
    	.byte	0xd
    	.byte	0x5
    	.byte	0x4
    	.4byte	.LCFI3-.LCFI1
    	.byte	0x2e
    	.byte	0x10
    	.byte	0x4
    	.4byte	.LCFI4-.LCFI3
    	.byte	0x2e
    	.byte	0x14
    	.byte	0x4
    	.4byte	.LCFI5-.LCFI4
    	.byte	0x2e
    	.byte	0x10
    	.byte	0x4
    	.4byte	.LCFI6-.LCFI5
    	.byte	0x2e
    	.byte	0x14
    	.byte	0x4
    	.4byte	.LCFI7-.LCFI6
    	.byte	0x2e
    	.byte	0x10
    	.byte	0x4
    	.4byte	.LCFI8-.LCFI7
    	.byte	0x2e
    	.byte	0x14
    	.byte	0x4
    	.4byte	.LCFI9-.LCFI8
    	.byte	0x2e
    	.byte	0x10
    	.byte	0x4
    	.4byte	.LCFI10-.LCFI9
    	.byte	0x2e
    	.byte	0x14
    	.byte	0x4
    	.4byte	.LCFI11-.LCFI10
    	.byte	0x2e
    	.byte	0x10
    	.byte	0x4
    	.4byte	.LCFI12-.LCFI11
    	.byte	0x2e
    	.byte	0x14
    	.byte	0x4
    	.4byte	.LCFI13-.LCFI12
    	.byte	0x2e
    	.byte	0x10
    	.align 4
    .LEFDE1:
    	.set	.LLFDE1,.LEFDE1-.LSFDE1
    	.ident	"GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.0)"
    is all asm for:
    Code:
    /*
     * main.c
     * String Experiment
     * written on Microsoft Visual C++ 6.0 Standard Edition
     * compiled with no errors
     * compiled on 4/23/02
     * Free to distribute and claim as your own!
     */
    ///////HEADERS//////////
    #include <iostream.h>
    #include <ctype.h>
    #include <string.h>
    ////END HEADERS/////////
    
    ////BEGIN MAIN////
    int main()
    {
    	// global declarations
    	int length;
    	int lengthb;
    	char firstName[20];
    	char lastName[20];
    	// end declarations
    	cout<<"Enter your first name: "; //prompt name
    	cin>>firstName;  // STDIN > firstName
    	
    	length = 0;  // Set length to 0
    	
    	while(firstName[length] != NULL)  // As long as there are non NULL Characters in firstName
    	{									//continue the loop
    
    		firstName[length] = tolower(firstName[length]); //Set all characters to lower case
    		length++; // length increases at auto increment
    	}
    	firstName[0] = toupper(firstName[0]); //Set first char of firstName to upper case
    
    	
    	lengthb = 0; // second length identification is set to 0 for While loop
    
    	cout<<endl<<"Enter your last name: "; //prompts for last name
    	cin>>lastName; // STDIN > lastName
    	while(lastName[lengthb] != NULL) // begin while loop
    	{
    		lastName[lengthb] = tolower(lastName[lengthb]); //Set all characters to lower case
    		lengthb++;  // second length declaration is set to auto increment in loop
    	}
    	lastName[0] = toupper(lastName[0]); // first character of lastName is set to upper case
    
    	cout<<endl<<firstName<<" "<<lastName<<endl; // display firstName lastName
    	cout<<endl<<"Your first name has "<<length<<" characters"<<endl; //display length
    	cout<<"Your last name has "<<lengthb<<" characters"<<endl; //display length(2)
    	cout<<"Your name has a total of "<<length+lengthb<<" characters"<<endl; //display the sum
    	
    	return 0; // return from main with no erros unless processor or kernel says otherwise
    } 
    ////END MAIN////
    Makes ya appreciate C++ eh? (Hopefully C more)
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Quick explanation of the asm
    it puts integers into memory addresses that can be called
    in a pattern called a stack
    stack of memory.

    With this memory in place you can allocate memory after it in patterns and order of functions to loops etc.

    the strings are stored at the top

    h = high memory address
    l = low memory address

    and I can't really explain the rest, if anyone knows asm feel free to explain as much as you can.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is not an ASM board.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest
    Forgive Quzah. He's been a little edgy since the incident with the airplanes. I enjoyed your post.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    Forgive Quzah. He's been a little edgy since the incident with the airplanes. I enjoyed your post.
    Airplanes? What airplanes? What are you talking about?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Maybe s/he was referring to Sayeh. Now that was a good flame war.

    Not as good as the Sunlight-Dean one though.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by golfinguy4
    Maybe s/he was referring to Sayeh. Now that was a good flame war.

    Not as good as the Sunlight-Dean one though.
    Hmm... I must have missed the S-D posts. Damn.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 02:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21