Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Jump table problems

ia32

  • This topic is locked This topic is locked
1 reply to this topic

#1 restin84

restin84

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, Assembly

Posted 19 October 2012 - 05:55 PM

Hi guys. I'm just starting to do assembly language for my Computer Organization class. I have to translate a given program into ia32 assembly language. We are required to use a jump table to solve the problem

#Name: Doug Tait
#Assignment3
#10.13.2012
/****************PROGRAM TO TRANSLATE**********************/
//#include <stdio.h>

/*int month, numDays, year;
int valid = 1;

int main() {

printf("Enter month as an integer (1-12): ");
scanf("%d", &month);
printf("Enter year (e.g., 2012): ");
scanf("%d", &year);

switch (month) {
	 case 1: case 3: case 5:
	 case 7: case 8: case 10:
	 case 12:
		 numDays = 31;
		 break;
	 case 4: case 6:
	 case 9: case 11:
		 numDays = 30;
		 break;
	 case 2:
		 if ( ((year % 4 == 0) &&
		 !(year % 100 == 0))
		 || (year % 400 == 0))
		 numDays = 29;
		 else
		 numDays = 28;
		 break;
	 default:
	 valid = 0;
		 printf("Invalid month.\n");
}
if (valid)
	 printf("%d/%d has %d days\n", month, year, numDays);
}*/
/************************************************************************/
/*************************INITIALIZED-GLOBAL-DATA************************/
.data
.align 4
valid: .long 1 #int valid = 1
/************************************************************************/
/************************UNINITIALIZED-GLOBAL-DATA***********************/
.comm month,4,4 #int month
.comm numDays,4,4 #int numDays
.comm year,4,4 #int year
/************************************************************************/
/****************************READ-ONLY-DATA******************************/
.section .rodata
.align 4
str1: .string "Enter month as an integer(1-12): "
frmt1: .string "%d"
str2: .string "Enter year (e.g., 2012): "
frmt2: .string "%d"
frmt3: .string "\n%d, %d"
str3: .string "31 days"
str4: .string "30 days"
str5: .string "29 or 28"
str6: .string "invalid month"
str7: .string "Program done"
/************************************************************************/
/******************************JUMP-TABLE********************************/
.align 4
.JT:
.long .L0 #January
.long .L1 #February

/************************************************************************/
/****************************MAIN-FUNCTION*******************************/
.text
.globl main #declare main as global
main:
pushl %ebp
movl %esp, %ebp
pushl $str1 #push address of frmt1 onto stack
call printf #call printf library function
addl $4, %esp #pop arguments off stack
pushl $month #push address of month onto stack
pushl $frmt1 #push address of frmt2 onto stack
call scanf #call scanf library function
addl $8, %esp #pop arguments off stack
pushl $str2 #push address of frmt3 onto stack
call printf #call printf library function
addl $4, %esp #pop arguments off stack
pushl $year #push address of year onto stack
pushl $frmt2 #push address of frmt4 onto stack
call scanf #call scanf library function
addl $8, %esp #pop arguments off stack

movl $month, %eax
subl $1,%eax
cmpl $11, %eax
ja .L12
jmp *.JT(,%eax,4)

.L0:
pushl $str3
call printf
addl $4, %esp
jmp .L13

.L1:
pushl $str5
call printf
addl $4, %esp
jmp .L13

.L12:
pushl $str6
call printf
addl $4, %esp

.L13:
pushl $str7
call printf
addl $4, %esp

pushl year
pushl month
pushl $frmt3
call printf
addl $8, %esp
leave
ret

I've been running into problems getting this thing working. Right now I am just trying to handle the month = 1 and month = 2 cases. When I get that working I will build from there. The program currently takes a month and year input successfully but jumps straight to the case saying "invalid month". I'm following examples from my text and lecture slides but I seem to be missing something. Does anyone have some advice?
  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 20 October 2012 - 02:36 PM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: ia32

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download